![]() |
Cinnamon
1.0
chess engine
|
00001 #ifndef THREAD_H_ 00002 #define THREAD_H_ 00003 00004 class Runnable { 00005 public: 00006 virtual void run() = 0; 00007 }; 00008 00009 #ifdef _WIN32 00010 00011 #include <windows.h> 00012 00013 class Thread: virtual public Runnable { 00014 private: 00015 HANDLE threadID; 00016 static DWORD WINAPI __run(void* cthis) { 00017 static_cast<Runnable*>(cthis)->run(); 00018 return 0; 00019 } 00020 00021 public: 00022 void start() { 00023 DWORD i; 00024 threadID=CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) __run,(LPVOID) this, 0, &i); 00025 } 00026 void join() { 00027 WaitForSingleObject(threadID, INFINITE); 00028 } 00029 void stop() { 00030 CloseHandle(threadID); 00031 } 00032 }; 00033 00034 #else 00035 00036 #include <pthread.h> 00037 00038 class Thread: virtual public Runnable { 00039 private: 00040 Runnable * _runnable; 00041 pthread_t threadID; 00042 static void * __run(void * cthis) { 00043 static_cast<Runnable*>(cthis)->run(); 00044 return NULL; 00045 } 00046 00047 public: 00048 Thread() : 00049 _runnable(NULL) { 00050 threadID=0; 00051 } 00052 00053 int start() { 00054 Runnable * execRunnable = this; 00055 if (this->_runnable != NULL) { 00056 execRunnable = this->_runnable; 00057 } 00058 return pthread_create(&threadID, NULL, __run, execRunnable); 00059 } 00060 void join() { 00061 pthread_join(threadID, NULL); 00062 } 00063 void stop() { 00064 if(threadID)pthread_detach(threadID) ; 00065 } 00066 }; 00067 #endif 00068 #endif