thread.hh
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef __GECODE_SUPPORT_THREAD_HH__
00039 #define __GECODE_SUPPORT_THREAD_HH__
00040
00041 #include <gecode/support.hh>
00042
00043 #ifdef GECODE_THREADS_WINDOWS
00044 #include <windows.h>
00045 #endif
00046
00047 #ifdef GECODE_THREADS_PTHREADS
00048 #include <pthread.h>
00049 #endif
00050
00051 #ifdef GECODE_HAS_THREADS
00052
00066 namespace Gecode { namespace Support {
00067
00075 class Runnable {
00076 public:
00078 virtual void run(void) = 0;
00080 virtual ~Runnable(void) {}
00082 static void* operator new(size_t s);
00084 static void operator delete(void* p);
00085 };
00086
00099 class Thread {
00100 private:
00101 #ifdef GECODE_THREADS_WINDOWS
00103 HANDLE w_h;
00104 #endif
00105 #ifdef GECODE_THREADS_PTHREADS
00107 pthread_t p_t;
00108 #endif
00109 public:
00116 GECODE_SUPPORT_EXPORT Thread(Runnable* r);
00118 ~Thread(void);
00120 static void sleep(unsigned int ms);
00122 static unsigned int npu(void);
00123 private:
00125 Thread(const Thread&) {}
00127 void operator=(const Thread&) {}
00128 };
00129
00130
00142 class Mutex {
00143 private:
00144 #ifdef GECODE_THREADS_WINDOWS
00146 CRITICAL_SECTION w_cs;
00147 #endif
00148 #ifdef GECODE_THREADS_PTHREADS
00150 pthread_mutex_t p_m;
00151 #endif
00152 public:
00154 Mutex(void);
00156 void acquire(void);
00158 bool tryacquire(void);
00160 void release(void);
00162 ~Mutex(void);
00163 private:
00165 Mutex(const Mutex&) {}
00167 void operator=(const Mutex&) {}
00168 };
00169
00177 class Lock {
00178 private:
00180 Mutex& m;
00181 public:
00183 Lock(Mutex& m0);
00185 ~Lock(void);
00186 private:
00188 Lock(const Lock& l) : m(l.m) {}
00190 void operator=(const Lock&) {}
00191 };
00192
00203 class Event {
00204 private:
00205 #ifdef GECODE_THREADS_WINDOWS
00207 HANDLE w_h;
00208 #endif
00209 #ifdef GECODE_THREADS_PTHREADS
00211 pthread_mutex_t p_m;
00213 pthread_cond_t p_c;
00215 bool p_s;
00216 #endif
00217 public:
00219 Event(void);
00221 void signal(void);
00223 void wait(void);
00225 ~Event(void);
00226 private:
00228 Event(const Event&) {}
00230 void operator=(const Event&) {}
00231 };
00232
00233 }}
00234
00235 #ifdef GECODE_THREADS_WINDOWS
00236 #include <gecode/support/thread/windows.hpp>
00237 #endif
00238 #ifdef GECODE_THREADS_PTHREADS
00239 #include <gecode/support/thread/pthreads.hpp>
00240 #endif
00241
00242 #include <gecode/support/thread/thread.hpp>
00243
00244
00245 #ifdef GECODE_THREADS_WINDOWS
00246 #ifdef min
00247 #undef min
00248 #endif
00249 #ifdef max
00250 #undef max
00251 #endif
00252 #endif
00253
00254 #endif
00255
00256 #endif
00257
00258