线程同步 win

来源:互联网 发布:java 验证日期格式 编辑:程序博客网 时间:2024/05/21 15:43


/*
 只能在当前进程中使用
 只能同步一个资源,要用同步多个资源需要使用  SynMultiWaite
*/
class Synchronization
{
 Synchronization(): m_dwMilliseconds(500){ m_Syn = CreateMutex(NULL,false,NULL);}
 Synchronization(DWORD dwMill): m_dwMilliseconds(500){ m_Syn = CreateMutex(NULL,false,NULL);}
 ~Synchronization() { CloseHandle(m_Syn);}
public:
/*
 WAIT_ABANDONED    
    The specified object is a mutex object that was not released
    by the thread that owned the mutex object before the owning
    thread terminated. Ownership of the mutex object is granted
    to the calling thread, and the mutex is set to nonsignaled.
 WAIT_OBJECT_0  
    The state of the specified object is signaled.
 WAIT_TIMEOUT    
    The time-out interval elapsed, and the object's state is nonsignaled.
*/
 DWORD SynSingleWaite(){  return WaitForSingleObject(m_Syn,m_ulMilliseconds); }
 bool SynRelease(){ return ReleaseMutex(m_Syn); }

 //when use mutl
 //DWORD SynMultiWaite(){ DWORD WaitForMultipleObjects();}
private:
 HANDLE m_Syn;
 DWORD m_dwMilliseconds;
};

0 0