mutex的使用类。
class CMutexEx
{
public:
CMutexEx() { m_hMutex = CreateMutex(NULL, FALSE, _T("TRANSACTION")); }
virtual ~CMutexEx() { Leave(); }
void Enter()
{
if(m_hMutex)
{
WaitForSingleObject(m_hMutex, INFINITE);
}
}
BOOL TryEnter(DWORD dwMilliseconds)
{
if(m_hMutex)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hMutex, dwMilliseconds))
{
return TRUE;
}
}
return FALSE;
}
void Leave()
{
if (m_hMutex)
{
ReleaseMutex(m_hMutex);
CloseHandle(m_hMutex);
}
}
private:
HANDLE m_hMutex;
};
阅读(2073) | 评论(0) | 转发(0) |