4 #ifndef USE_CPP_THREADS 5 #ifndef USE_BOOST_THREADS 9 #define USE_CPP_THREADS 1 10 #define USE_BOOST_THREADS 0 11 #define USE_PTHREADS 0 13 #define USE_CPP_THREADS 0 14 #define USE_BOOST_THREADS 1 15 #define USE_PTHREADS 0 18 #define USE_CPP_THREADS 0 19 #define USE_BOOST_THREADS 0 20 #define USE_PTHREADS 1 23 #endif //USE_BOOST_THREADS 24 #endif //USE_CPP_THREADS 29 #include <condition_variable> 32 typedef std::thread Thread;
33 typedef std::mutex
Mutex;
40 typedef std::condition_variable
Condition;
41 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data = NULL) {
return std::thread(fn, data); }
42 inline void ThreadJoin(Thread& thread) { thread.join(); }
43 inline void ThreadYield() { std::this_thread::yield(); }
45 #endif //USE_CPP_THREADS 48 #include <boost/thread.hpp> 49 #include <boost/interprocess/sync/interprocess_condition.hpp> 50 typedef boost::thread Thread;
51 typedef boost::mutex
Mutex;
53 typedef boost::interprocess::interprocess_condition Condition;
54 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data=NULL) {
return boost::thread(fn,data); }
55 inline void ThreadJoin(Thread& thread) { thread.join(); }
56 inline void ThreadYield() { boost::this_thread::yield(); }
58 #endif //USE_BOOST_THREADS 62 typedef pthread_t Thread;
63 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data=NULL) {
65 pthread_create(&thread,NULL,fn,data);
68 inline void ThreadJoin(Thread& thread) { pthread_join(thread,NULL); }
69 inline void ThreadYield() { pthread_yield(); }
72 Mutex() { mutex = PTHREAD_MUTEX_INITIALIZER; }
73 ~Mutex() { pthread_mutex_destroy(&mutex); }
74 void lock() { pthread_mutex_lock(&mutex); }
75 bool try_lock() {
return (pthread_mutex_lock(&mutex) == 0); }
76 void unlock() { pthread_mutex_unlock(&mutex); }
77 pthread_mutex_t mutex;
82 ScopedLock(Mutex& _mutex) :mutex(_mutex) { pthread_mutex_lock(&mutex.mutex); }
83 ~ScopedLock() { pthread_mutex_unlock(&mutex.mutex); }
89 Condition() { cond = PTHREAD_COND_INITIALIZER; }
90 ~Condition() { pthread_cond_destroy(&cond); }
91 void wait(ScopedLock& lock) { pthread_cond_wait(&cond,&lock.mutex.mutex); }
92 void notify_one() { pthread_cond_signal(&cond); }
93 void notify_all() { pthread_cond_broadcast(&cond); }
101 void ThreadSleep(
double duration);
104 inline void ThreadSleep(
double duration) { usleep(
int(duration*1000000)); }
107 #endif //THREAD_UTILS_H Definition: threadutils.h:87
Definition: threadutils.h:80
Definition: threadutils.h:70