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) 25 template <
class CLASS,
int (CLASS::*PROC)(
void)>
static Thread 26 ThreadCreateObjectFunctor(CLASS *pthis) {
27 return createThread(createThreadAux<CLASS, PROC>, pthis);
30 template <
class CLASS,
int (CLASS::*PROC)(
void) >
static _size_t THREAD_PROC
31 createThreadAux(
void *param) {
32 return (static_cast<CLASS *>(param)->*PROC)();
35 static Thread createThread(thread_proc_t proc,
void *param = NULL) {
36 Thread thread_(proc, param);
38 thread_._handle = (_size_t)(_beginthreadex(NULL, 0,
39 (
unsigned int (__stdcall *)(
void *))proc, param, 0, NULL));
41 assert(
sizeof(thread_._handle) >=
sizeof(pthread_t));
43 pthread_create((pthread_t *)&thread_._handle, NULL, (
void *(*)(
void *))proc,
50 explicit Thread(): _param(NULL), _func(NULL), _handle(0) {}
62 if (TerminateThread(reinterpret_cast<HANDLE>(this->_handle), -1)) {
63 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
76 return pthread_cancel((pthread_t)this->_handle);
82 int join(
unsigned long timeout = -1) {
89 switch (WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout)) {
91 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
106 s = pthread_cancel((pthread_t)(this->_handle));
111 s = pthread_join((pthread_t)(this->_handle), &res);
116 if (res == PTHREAD_CANCELED) {
117 printf(
"%lu thread has been canceled\n", this->_handle);
125 bool operator== (
const Thread &right) {
126 return this->_handle == right._handle;
129 explicit Thread(thread_proc_t proc,
void *param): _param(param), _func(proc),