歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux平台用C++實現事件對象,同步線程

Linux平台用C++實現事件對象,同步線程

日期:2017/3/1 10:46:47   编辑:Linux編程

前文在Win32平台上用C++實現了事件對象Event,對線程進行同步(見 http://www.linuxidc.com/Linux/2011-12/49717.htm ),以達到期望目的。這次在Linux平台上實現與之類似的事件對象。與其相關的一組API包括:pthread_mutex_init,pthread_cond_init,pthread_mutex_lock,pthread_cond_wait,pthread_mutex_unlock,pthread_cond_broadcast,pthread_cond_timedwait,pthread_cond_destroy,pthread_mutex_destroy。下邊,是封裝的事件對象類,以及測試代碼。使用VS2005編輯,在虛擬機 Fedora 13中編譯,測試通過。

MyEvent.h

  1. #ifndef My_Event_Header
  2. #define My_Event_Header
  3. #include <iostream>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. using namespace std;
  7. //---------------------------------------------------------------
  8. class CEventImpl
  9. {
  10. protected:
  11. /*
  12. 動態方式初始化互斥鎖,初始化狀態變量m_cond
  13. `bAutoReset true 人工重置
  14. false 自動重置
  15. */
  16. CEventImpl(bool manualReset);
  17. /*
  18. 注銷互斥鎖,注銷狀態變量m_cond
  19. */
  20. ~CEventImpl();
  21. /*
  22. 將當前事件對象設置為有信號狀態
  23. 若自動重置,則等待該事件對象的所有線程只有一個可被調度
  24. 若人工重置,則等待該事件對象的所有線程變為可被調度
  25. */
  26. void SetImpl();
  27. /*
  28. 以當前事件對象,阻塞線程,將其永遠掛起
  29. 直到事件對象被設置為有信號狀態
  30. */
  31. bool WaitImpl();
  32. /*
  33. 以當前事件對象,阻塞線程,將其掛起指定時間間隔
  34. 之後線程自動恢復可調度
  35. */
  36. bool WaitImpl(long milliseconds);
  37. /*
  38. 將當前事件對象設置為無信號狀態
  39. */
  40. void ResetImpl();
  41. private:
  42. bool m_manual;
  43. volatile bool m_state;
  44. pthread_mutex_t m_mutex;
  45. pthread_cond_t m_cond;
  46. };
  47. inline void CEventImpl::SetImpl()
  48. {
  49. if (pthread_mutex_lock(&m_mutex))
  50. cout<<"cannot signal event (lock)"<<endl;
  51. //設置狀態變量為true,對應有信號
  52. m_state = true;
  53. //cout<<"CEventImpl::SetImpl m_state = "<<m_state<<endl;
  54. //重新激活所有在等待m_cond變量的線程
  55. if (pthread_cond_broadcast(&m_cond))
  56. {
  57. pthread_mutex_unlock(&m_mutex);
  58. cout<<"cannot signal event"<<endl;
  59. }
  60. pthread_mutex_unlock(&m_mutex);
  61. }
  62. inline void CEventImpl::ResetImpl()
  63. {
  64. if (pthread_mutex_lock(&m_mutex))
  65. cout<<"cannot reset event"<<endl;
  66. //設置狀態變量為false,對應無信號
  67. m_state = false;
  68. //cout<<"CEventImpl::ResetImpl m_state = "<<m_state<<endl;
  69. pthread_mutex_unlock(&m_mutex);
  70. }
  71. //---------------------------------------------------------------
  72. class CMyEvent: private CEventImpl
  73. {
  74. public:
  75. CMyEvent(bool bManualReset = true);
  76. ~CMyEvent();
  77. void Set();
  78. bool Wait();
  79. bool Wait(long milliseconds);
  80. bool TryWait(long milliseconds);
  81. void Reset();
  82. private:
  83. CMyEvent(const CMyEvent&);
  84. CMyEvent& operator = (const CMyEvent&);
  85. };
  86. inline void CMyEvent::Set()
  87. {
  88. SetImpl();
  89. }
  90. inline bool CMyEvent::Wait()
  91. {
  92. return WaitImpl();
  93. }
  94. inline bool CMyEvent::Wait(long milliseconds)
  95. {
  96. if (!WaitImpl(milliseconds))
  97. {
  98. cout<<"time out"<<endl;
  99. return false;
  100. }
  101. else
  102. {
  103. return true;
  104. }
  105. }
  106. inline bool CMyEvent::TryWait(long milliseconds)
  107. {
  108. return WaitImpl(milliseconds);
  109. }
  110. inline void CMyEvent::Reset()
  111. {
  112. ResetImpl();
  113. }
  114. #endif
Copyright © Linux教程網 All Rights Reserved