00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LIBPLANETCTHREAD_H
00020 #define LIBPLANETCTHREAD_H
00021
00022 #if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS)
00023
00024 #define USE_WIN32_THREADS
00025 #undef USE_POSIX_THREADS
00026
00027 #else
00028
00029 #undef USE_WIN32_THREADS
00030 #define USE_POSIX_THREADS
00031
00032 #endif
00033
00034
00035 #ifdef USE_POSIX_THREADS
00036 #include <pthread.h>
00037 #endif
00038 #ifdef USE_WIN32_THREADS
00039 #include "windows-includes.h"
00040 #endif
00041
00042 #include <vector>
00043
00044 namespace libProcTer {
00045
00067 class CThread
00068 {
00069 public:
00071 CThread();
00073 virtual ~CThread();
00074
00078 bool start();
00083 bool stop();
00085 bool isRunning();
00086
00091 void makeLowPriority();
00092
00105 virtual void threadFunc()=0;
00106
00107 #ifdef USE_POSIX_THREADS
00108 static void *run(void *arg);
00109 #endif
00110 #ifdef USE_WIN32_THREADS
00111 static DWORD WINAPI run(void *arg);
00112 #endif
00113
00114 protected:
00115
00121 bool m_Terminate;
00122
00123 private:
00124 #ifdef USE_POSIX_THREADS
00125 pthread_t m_ThreadID;
00126 #endif
00127 #ifdef USE_WIN32_THREADS
00128 HANDLE m_ThreadHandle;
00129 #endif
00130 };
00131
00173 class CCriticalSection
00174 {
00175 public:
00177 CCriticalSection();
00179 ~CCriticalSection();
00180
00185 void lock();
00186
00190 void unlock();
00191
00192 private:
00193 #ifdef USE_POSIX_THREADS
00194 pthread_mutex_t m_Mutex;
00195 #endif
00196 #ifdef USE_WIN32_THREADS
00197 CRITICAL_SECTION m_Section;
00198 #endif
00199 };
00200
00210 template<typename T>
00211 class CCriticalVector : public std::vector<T>, public CCriticalSection
00212 {
00213 };
00214
00215 }
00216
00217 #endif