00001 00037 #ifndef _THREAD_H 00038 #define _THREAD_H 00039 00040 #include "defines.h" 00041 00042 #if defined(_WINDOWS) || defined(_WIN32_WCE) 00043 # define THREAD DWORD WINAPI 00044 #elif defined(_SYMBIAN) 00045 # define THREAD TInt 00046 #else 00047 00048 # define THREAD void* 00049 #endif 00050 00054 class Thread 00055 { 00056 protected: 00060 class ThreadHandle 00061 { 00062 friend class Thread; 00063 00064 private: 00066 void* nativeHandle; 00067 00068 public: 00069 ThreadHandle(); 00070 ~ThreadHandle(); 00071 }; 00072 00076 class Lock 00077 { 00078 private: 00080 void* nativeLock; 00081 00082 public: 00084 Lock(); 00086 ~Lock(); 00087 00089 void lock(); 00091 void unlock(); 00092 }; 00093 00097 class Guard { 00098 private: 00100 Lock& lock; 00101 00102 public: 00108 Guard(Lock& lock); 00110 ~Guard(); 00111 }; 00112 00113 private: 00115 00116 bool cancelled; 00117 bool started; 00118 ThreadHandle handle; 00119 Lock lockShouldTerminate; 00121 00128 static THREAD runProc(void *userData); 00129 00130 public: 00132 Thread(); 00134 virtual ~Thread(); 00135 00141 static void sleep(unsigned milliseconds); 00142 00144 void start(); 00146 void cancel(); 00148 void join(); 00150 void terminate(); 00151 00152 00153 protected: 00157 virtual void run() = 0; 00158 00163 bool isCancelled(); 00164 }; 00165 00166 00167 #endif