用C++实现Win32事件对象,同步线程


在Win32环境下编写多线程应用程序,经常要用到事件对象Event,来进行线程同步。与其相关的一组API包括:CreateEvent,SetEvent,ResetEvent,WaitForSingleObject,和CloseHandle。关于这些API的功能以及参数意义等这里就不多说了。下边,我封装了一个事件对象类,以及测试代码。已由本人在VS2005环境下编译,测试通过。

MyEvent.h

  1. #ifndef My_Event_Header   
  2. #define My_Event_Header   
  3.   
  4. #include <iostream>   
  5.   
  6. #ifdef WIN32   
  7. #include <Windows.h>   
  8. #endif   
  9.   
  10. using namespace std;  
  11.   
  12. //---------------------------------------------------------------   
  13.   
  14. class CEventImpl  
  15. {  
  16. protected:  
  17.       
  18.     /* 
  19.      创建一个匿名事件对象 
  20.     `bAutoReset  true   人工重置 
  21.                  false  自动重置 
  22.     */  
  23.     CEventImpl(bool bManualReset);        
  24.       
  25.     /* 
  26.      销毁一个事件对象 
  27.     */  
  28.     ~CEventImpl();  
  29.   
  30.     /* 
  31.      将当前事件对象设置为有信号状态 
  32.      若自动重置,则等待该事件对象的所有线程只有一个可被调度 
  33.      若人工重置,则等待该事件对象的所有线程变为可被调度 
  34.     */  
  35.     void SetImpl();  
  36.   
  37.     /* 
  38.      以当前事件对象,阻塞线程,将其永远挂起 
  39.      直到事件对象被设置为有信号状态 
  40.     */  
  41.     bool WaitImpl();  
  42.   
  43.     /* 
  44.      以当前事件对象,阻塞线程,将其挂起指定时间间隔 
  45.      之后线程自动恢复可调度 
  46.     */  
  47.     bool WaitImpl(long lMilliseconds);  
  48.   
  49.     /* 
  50.      将当前事件对象设置为无信号状态 
  51.     */  
  52.     void ResetImpl();  
  53.   
  54. private:  
  55.     HANDLE h_Event;  
  56. };  
  57.   
  58. inline void CEventImpl::SetImpl()  
  59. {  
  60.     if (!SetEvent(h_Event))  
  61.     {  
  62.         cout<<"cannot signal event"<<endl;  
  63.     }  
  64. }  
  65.   
  66. inline void CEventImpl::ResetImpl()  
  67. {  
  68.     if (!ResetEvent(h_Event))  
  69.     {  
  70.         cout<<"cannot reset event"<<endl;  
  71.     }  
  72. }  
  73.   
  74. //---------------------------------------------------------------   
  75.   
  76. class CMyEvent: private CEventImpl  
  77. {  
  78. public:  
  79.     CMyEvent(bool bManualReset = true);  
  80.     ~CMyEvent();  
  81.   
  82.     void Set();  
  83.     bool Wait();  
  84.     bool Wait(long milliseconds);  
  85.     bool TryWait(long milliseconds);  
  86.     void Reset();  
  87.   
  88. private:  
  89.     CMyEvent(const CMyEvent&);  
  90.     CMyEvent& operator = (const CMyEvent&);  
  91. };  
  92.   
  93.   
  94. inline void CMyEvent::Set()  
  95. {  
  96.     SetImpl();  
  97. }  
  98.   
  99. inline bool CMyEvent::Wait()  
  100. {  
  101.     return WaitImpl();  
  102. }  
  103.   
  104. inline bool CMyEvent::Wait(long milliseconds)  
  105. {  
  106.     if (!WaitImpl(milliseconds))  
  107.     {  
  108.         cout<<"time out"<<endl;  
  109.         return false;  
  110.     }  
  111.     else  
  112.     {  
  113.         return true;  
  114.     }  
  115. }  
  116.   
  117. inline bool CMyEvent::TryWait(long milliseconds)  
  118. {  
  119.     return WaitImpl(milliseconds);  
  120. }  
  121.   
  122. inline void CMyEvent::Reset()  
  123. {  
  124.     ResetImpl();  
  125. }  
  126.   
  127. #endif  
  • 1
  • 2
  • 下一页

相关内容