mirror of https://github.com/tongzx/nt5src
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
967 B
53 lines
967 B
#ifndef THREADLOCK_H
|
|
#define THREADLOCK_H
|
|
class CThreadLock
|
|
{
|
|
public:
|
|
CThreadLock()
|
|
{
|
|
InitializeCriticalSection(&_csAccess);
|
|
}
|
|
~CThreadLock()
|
|
{
|
|
DeleteCriticalSection(&_csAccess);
|
|
}
|
|
void ThreadLock()
|
|
{
|
|
EnterCriticalSection(&_csAccess);
|
|
}
|
|
void ThreadUnLock()
|
|
{
|
|
LeaveCriticalSection(&_csAccess);
|
|
}
|
|
private:
|
|
CRITICAL_SECTION _csAccess; // Used to control access to member data
|
|
|
|
// Do not allow this object to be copied.
|
|
CThreadLock(const CThreadLock &that);
|
|
operator=(const CThreadLock &that);
|
|
};
|
|
class CAutoLock
|
|
{
|
|
public:
|
|
CAutoLock(CThreadLock *pThis) : _pThis(pThis)
|
|
{
|
|
_pThis->ThreadLock();
|
|
}
|
|
~CAutoLock()
|
|
{
|
|
_pThis->ThreadUnLock();
|
|
}
|
|
|
|
private:
|
|
CThreadLock *_pThis;
|
|
};
|
|
|
|
//
|
|
//
|
|
// LOCK_LOCALS() should be used whenever access to thread-safe member data
|
|
// is needed.
|
|
//
|
|
#define LOCK_LOCALS(pObj) CAutoLock local_lock(pObj);
|
|
|
|
#endif
|
|
|