封装个简单的自动锁
#pragma once
//锁接口
class ILock
{
public:
virtual ~ILock() {}
virtual void Lock() = 0;
virtual void Unlock() = 0;
};
/************************************************************************/
/* CRITICAL_SECTION */
/************************************************************************/
class CriticalSectionLocker
: ILock
{
public:
inline CriticalSectionLocker(void)
{
InitializeCriticalSection(&m_hCriticalSection);
}
inline virtual ~CriticalSectionLocker(void)
{
DeleteCriticalSection(&m_hCriticalSection);
}
inline virtual void Lock()
{
EnterCriticalSection(&m_hCriticalSection);
}
inline virtual void Unlock()
{
LeaveCriticalSection(&m_hCriticalSection);
}
private:
CRITICAL_SECTION m_hCriticalSection;
};
/************************************************************************/
/* Mutex */
/************************************************************************/
//互斥对象锁类
class MutexLocker
: public ILock
{
public:
MutexLocker()
{
m_mutex = ::CreateMutex(NULL, FALSE, NULL);
}
~MutexLocker()
{
::CloseHandle(m_mutex);
}
inline virtual void Lock()
{
DWORD d = WaitForSingleObject(m_mutex, INFINITE);
}
inline virtual void Unlock()
{
::ReleaseMutex(m_mutex);
}
private:
HANDLE m_mutex;
};
/************************************************************************/
/* 自动锁 */
/************************************************************************/
template <typename LockTyp>
class AutoLock
{
LockTyp* m_pLock;
public:
AutoLock(LockTyp& locker)
{
m_pLock = &locker;
m_pLock->Lock();
}
AutoLock(LockTyp* locker)
{
m_pLock = locker;
m_pLock->Lock();
}
~AutoLock(void)
{
m_pLock->Unlock();
}
};
//锁接口
class ILock
{
public:
virtual ~ILock() {}
virtual void Lock() = 0;
virtual void Unlock() = 0;
};
/************************************************************************/
/* CRITICAL_SECTION */
/************************************************************************/
class CriticalSectionLocker
: ILock
{
public:
inline CriticalSectionLocker(void)
{
InitializeCriticalSection(&m_hCriticalSection);
}
inline virtual ~CriticalSectionLocker(void)
{
DeleteCriticalSection(&m_hCriticalSection);
}
inline virtual void Lock()
{
EnterCriticalSection(&m_hCriticalSection);
}
inline virtual void Unlock()
{
LeaveCriticalSection(&m_hCriticalSection);
}
private:
CRITICAL_SECTION m_hCriticalSection;
};
/************************************************************************/
/* Mutex */
/************************************************************************/
//互斥对象锁类
class MutexLocker
: public ILock
{
public:
MutexLocker()
{
m_mutex = ::CreateMutex(NULL, FALSE, NULL);
}
~MutexLocker()
{
::CloseHandle(m_mutex);
}
inline virtual void Lock()
{
DWORD d = WaitForSingleObject(m_mutex, INFINITE);
}
inline virtual void Unlock()
{
::ReleaseMutex(m_mutex);
}
private:
HANDLE m_mutex;
};
/************************************************************************/
/* 自动锁 */
/************************************************************************/
template <typename LockTyp>
class AutoLock
{
LockTyp* m_pLock;
public:
AutoLock(LockTyp& locker)
{
m_pLock = &locker;
m_pLock->Lock();
}
AutoLock(LockTyp* locker)
{
m_pLock = locker;
m_pLock->Lock();
}
~AutoLock(void)
{
m_pLock->Unlock();
}
};