![]() |
Cinnamon
1.1
chess engine
|
00001 #ifndef THREAD_H_ 00002 #define THREAD_H_ 00003 #include <thread> 00004 class Runnable { 00005 public: 00006 virtual void run() = 0; 00007 }; 00008 00009 class Thread: virtual public Runnable { 00010 private: 00011 thread* theThread; 00012 Runnable * _runnable; 00013 Runnable * execRunnable; 00014 static void * __run(void * cthis) { 00015 static_cast<Runnable*>(cthis)->run(); 00016 return nullptr; 00017 } 00018 public: 00019 Thread() : 00020 _runnable(nullptr) { 00021 theThread = nullptr; 00022 execRunnable = this; 00023 } 00024 00025 void start() { 00026 ASSERT(!theThread); 00027 if (this->_runnable != nullptr) { 00028 execRunnable = this->_runnable; 00029 } 00030 theThread=new thread(__run,execRunnable); 00031 } 00032 00033 void join() { 00034 if(theThread) { 00035 theThread->join(); 00036 delete theThread; 00037 theThread=nullptr; 00038 } 00039 } 00040 00041 bool isJoinable() { 00042 return theThread->joinable(); 00043 } 00044 00045 void stop() { 00046 if(theThread) { 00047 theThread->detach(); 00048 delete theThread; 00049 theThread=nullptr; 00050 } 00051 } 00052 00053 ~Thread() { 00054 if(theThread) { 00055 theThread->detach(); 00056 delete theThread; 00057 } 00058 } 00059 }; 00060 #endif 00061 00062