19 #ifndef __THREAD_H__BY_SGCHOI
20 #define __THREAD_H__BY_SGCHOI
32 CEvent(BOOL bManualReset = FALSE, BOOL bInitialSet = FALSE)
34 m_hHandle = ::CreateEvent(0, bManualReset, bInitialSet, 0);
39 CloseHandle(m_hHandle);
44 BOOL Set() {
return SetEvent(m_hHandle);}
45 BOOL Reset() {
return ResetEvent(m_hHandle);}
46 BOOL Wait() {
return WaitForSingleObject(m_hHandle, INFINITE) == WAIT_OBJECT_0;}
60 CLock() {InitializeCriticalSection(&m_cs);}
61 ~
CLock() {DeleteCriticalSection(&m_cs);}
64 void Lock() {EnterCriticalSection(&m_cs);}
65 void Unlock() {LeaveCriticalSection(&m_cs);}
68 CRITICAL_SECTION m_cs;
74 CThread() {m_bRunning = FALSE; m_hHandle = NULL;}
75 virtual ~
CThread() {
if(m_hHandle != NULL) CloseHandle(m_hHandle);}
81 m_hHandle = CreateThread(0, 0, ThreadMainHandler,
this,0,0);
82 if( m_hHandle == NULL )
90 if( !m_bRunning )
return TRUE;
91 return WaitForSingleObject(m_hHandle, INFINITE) == WAIT_OBJECT_0;
96 if( !m_bRunning)
return TRUE;
98 m_bRunning = !(TerminateThread(m_hHandle, 0));
108 virtual void ThreadMain() = 0;
110 static DWORD __stdcall ThreadMainHandler(
void* p )
114 pThis->m_bRunning = FALSE;
135 m_bRunning = !pthread_create(&m_pThread, NULL, ThreadMainHandler, (
void*)
this);
142 return pthread_join(m_pThread, NULL) == 0;
157 virtual void ThreadMain() = 0;
158 static void* ThreadMainHandler(
void* p) {
161 pThis->m_bRunning = FALSE;
174 pthread_mutex_init(&m_mtx, NULL);
177 pthread_mutex_destroy(&m_mtx);
182 pthread_mutex_lock(&m_mtx);
185 pthread_mutex_unlock(&m_mtx);
189 pthread_mutex_t m_mtx;
195 CEvent(BOOL bManualReset = FALSE, BOOL bInitialSet = FALSE) {
196 pthread_mutex_init(&m_mtx, NULL);
197 pthread_cond_init(&m_cnd, NULL);
198 m_bManual = bManualReset;
199 m_bSet = bInitialSet;
203 pthread_mutex_destroy(&m_mtx);
204 pthread_cond_destroy(&m_cnd);
210 pthread_mutex_lock(&m_mtx);
214 pthread_cond_signal(&m_cnd);
217 pthread_mutex_unlock(&m_mtx);
222 pthread_mutex_lock(&m_mtx);
225 pthread_cond_wait(&m_cnd, &m_mtx);
230 pthread_mutex_unlock(&m_mtx);
235 pthread_mutex_lock(&m_mtx);
237 pthread_mutex_unlock(&m_mtx);
241 pthread_cond_t m_cnd;
242 pthread_mutex_t m_mtx;
262 #endif //__THREAD_H__BY_SGCHOI