14 #define UNUSED(x) (void)x 16 #if defined(__ANDROID__) 17 #define pthread_cancel(x) 0 20 #define CLASS_THREAD(c , x ) Thread::ThreadCreateObjectFunctor<c, &c::x>(this) 26 template <
class CLASS,
int (CLASS::*PROC)(
void)>
static Thread ThreadCreateObjectFunctor(CLASS * pthis){
27 return createThread(createThreadAux<CLASS,PROC>, pthis);
30 template <
class CLASS,
int (CLASS::*PROC)(
void) >
static _size_t THREAD_PROC createThreadAux(
void * param){
31 return (static_cast<CLASS *>(param)->*PROC)();
34 static Thread createThread(thread_proc_t proc,
void * param = NULL ){
35 Thread thread_(proc, param);
37 thread_._handle = (_size_t)( _beginthreadex(NULL, 0, (
unsigned int (__stdcall * )(
void * ))proc, param, 0, NULL));
39 assert(
sizeof(thread_._handle) >=
sizeof(pthread_t));
41 pthread_create((pthread_t *)&thread_._handle, NULL, (
void * (*)(
void *))proc, param);
47 explicit Thread(): _param(NULL),_func(NULL),_handle(0){}
57 if (TerminateThread( reinterpret_cast<HANDLE>(this->_handle), -1)){
58 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
66 if (!this->_handle)
return 0;
68 return pthread_cancel((pthread_t)this->_handle);
74 int join(
unsigned long timeout = -1){
79 switch ( WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout)){
81 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
91 pthread_join((pthread_t)(this->_handle), NULL);
96 bool operator== (
const Thread & right) {
97 return this->_handle == right._handle;
100 explicit Thread( thread_proc_t proc,
void * param ):_param(param),_func(proc), _handle(0){}