Browse
 
Tools
Rss Categories

headerClasses.h

Reference Number: AA-01512 Views: 6739 0 Rating/ Voters

C++ Code

  1. #ifndef HEADER_ONLY_HELPER_CLASSES_DEFINED
  2. #define HEADER_ONLY_HELPER_CLASSES_DEFINED
  3. #include <string>
  4. #include <process.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/timeb.h>
  8. #include <Windows.h>
  9. #undef GetObject
  10.  
  11. namespace Demo
  12. {
  13. // Critical section wrapper
  14. class CS
  15. {
  16. public:
  17. CS(): m_busy(false)
  18. {
  19. InitializeCriticalSection( &m_cs );
  20. }
  21. virtual ~CS()
  22. {
  23. DeleteCriticalSection( &m_cs );
  24. }
  25. bool IsBusy() const { return m_busy; } // Only valid at time of call
  26.  
  27. void Enter()
  28. {
  29. EnterCriticalSection( &m_cs );
  30. m_busy = true;
  31. }
  32. void Leave()
  33. {
  34. // Be careful, linux allows other non-owner of cs to unlock
  35. m_busy = false;
  36. LeaveCriticalSection( &m_cs );
  37. }
  38. bool Try()
  39. {
  40. if (m_busy)
  41. return false;
  42. Enter();
  43. return true;
  44. }
  45. private:
  46. volatile bool m_busy;
  47. CRITICAL_SECTION m_cs;
  48. };
  49.  
  50. // Simple way to lock critical section (releases in destructor)
  51. class CSLock
  52. {
  53. public:
  54. CSLock(CS& cs)
  55. {
  56. m_localCs = &cs;
  57. m_localCs->Enter();
  58. }
  59. virtual ~CSLock()
  60. {
  61. m_localCs->Leave();
  62. }
  63. private:
  64. CS* m_localCs;
  65. };
  66.  
  67. // Simple windows event wrapper
  68. class Event
  69. {
  70. public:
  71. Event()
  72. {
  73. m_event = CreateEvent(NULL, false, false, NULL);
  74. }
  75. virtual ~Event()
  76. {
  77. CloseHandle( m_event );
  78. }
  79. bool Wait(unsigned int timeout = INFINITE)
  80. {
  81. return WaitForSingleObject( m_event, timeout ) != WAIT_TIMEOUT;
  82. }
  83. bool Reset()
  84. {
  85. return ResetEvent( m_event ) != 0;
  86. }
  87. bool Signal()
  88. {
  89. return SetEvent( m_event ) != 0;
  90. }
  91. bool Try()
  92. {
  93. return Wait(0);
  94. }
  95. private:
  96. HANDLE m_event;
  97. };
  98.  
  99. // A thread class. Have your class derive from this one, override the Thread() function.
  100. class Thread
  101. {
  102. bool            Running;
  103. bool            ShuttingDown;
  104. bool            InUserThread;
  105. HANDLE          hThread;
  106. unsigned int    thrdaddr;
  107. CS              CS;
  108. Event           Event;
  109. public:
  110. Thread()
  111. {
  112. Running = false;
  113. ShuttingDown = true;
  114. InUserThread = false;
  115. }
  116.  
  117. virtual ~Thread(){ ThreadStop(); }
  118. virtual void ThreadAction() = 0; // Derive and override the ThreadAction function
  119. bool ThreadActivate()
  120. {
  121. CSLock L(CS);
  122. if (Running) return false;
  123. ShuttingDown = false;
  124. Running = true;
  125. InUserThread = false;
  126. hThread = (HANDLE) _beginthreadex(NULL, 0, CallBackThread ,(LPVOID) this, 0, &thrdaddr);
  127. return true;
  128. }
  129. bool ThreadStart()
  130. {
  131. CSLock L(CS);
  132. if (!Running || ShuttingDown  || InUserThread) return false;
  133. Event.Signal();
  134. return true;
  135. }
  136. bool ThreadStop(unsigned long WaitTime = 1000)
  137. {
  138. {
  139. CSLock L(CS);
  140. if (!Running) return false;
  141.  
  142. ShuttingDown = true;
  143. Event.Signal();
  144. }
  145.  
  146. if (WaitForSingleObject(hThread, WaitTime) == WAIT_TIMEOUT)
  147. TerminateThread(hThread,0);
  148.  
  149. Sleep(50);
  150. thrdaddr = 0;
  151. Running = false;
  152. return true;
  153. }
  154. bool IsThreadRunning(){CSLock L(CS);return Running;};
  155. bool IsThreadShuttingDown(){CSLock L(CS);return ShuttingDown;};
  156.  
  157. private:
  158. static unsigned int __stdcall CallBackThread(void* p)
  159. {
  160. ((Thread*)p)->InternalThread();
  161. return 0;
  162. }
  163. void InternalThread()
  164. {
  165. while (!ShuttingDown)
  166. {
  167. if (Event.Wait(2000))
  168. {
  169. {
  170. CSLock L(CS);
  171. InUserThread = true;
  172. }
  173. ThreadAction();
  174. {
  175. CSLock L(CS);
  176. InUserThread = false;
  177. }
  178. }
  179. }
  180. {
  181. CSLock L(CS);
  182. Running = false;
  183. }
  184. }
  185. };
  186. }// Namespace Demo
  187. #endif