1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#if defined(__WXMSW__)
#include "ethread_win.h"
#include <process.h>
#include "ethread.h"
static unsigned __stdcall startFunc(void* arg)
{
eThread *thread = reinterpret_cast<eThread*>(arg);
if(thread) {
// call the thread main loop
thread->start();
}
// terminate the thread
_endthreadex( 0 );
return 0;
}
eThreadImpl::eThreadImpl()
: m_stopEvent(INVALID_HANDLE_VALUE)
, m_handle(INVALID_HANDLE_VALUE)
{
m_stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
}
eThreadImpl::~eThreadImpl()
{
CloseHandle(m_stopEvent);
m_stopEvent = INVALID_HANDLE_VALUE;
}
void eThreadImpl::run(eThread *thread)
{
m_handle = (HANDLE)_beginthreadex
(
NULL, // default security
0, // default stack size
&startFunc, // entry point
thread,
0,
(unsigned int *)&m_tid
);
}
bool eThreadImpl::testDestroy()
{
DWORD dwRet = WaitForSingleObject(m_stopEvent, 0);
if(dwRet == WAIT_OBJECT_0) {
// the event is signaled
return true;
}
return false;
}
void eThreadImpl::requestStop()
{
SetEvent(m_stopEvent);
}
void eThreadImpl::wait(long timeout)
{
if(m_handle != INVALID_HANDLE_VALUE){
if(WaitForSingleObject(m_handle, timeout) == WAIT_OBJECT_0){
CloseHandle(m_handle);
m_handle = INVALID_HANDLE_VALUE;
}
}
}
#endif // __WXMSW__
|