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;
36 ~
Mutex() { mutex.~mutex(); }
37 void lock() { mutex.lock(); }
38 bool trylock() {
return (mutex.try_lock() == 0); }
39 void unlock() { mutex.unlock(); }
47 typedef std::condition_variable
Condition;
48 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data = NULL) {
return std::thread(fn, data); }
49 inline void ThreadJoin(Thread& thread) { thread.join(); }
50 inline void ThreadYield() { std::this_thread::yield(); }
52 #endif //USE_CPP_THREADS 55 #include <boost/thread.hpp> 56 #include <boost/interprocess/sync/interprocess_condition.hpp> 57 typedef boost::thread Thread;
58 typedef boost::mutex
Mutex;
60 typedef boost::interprocess::interprocess_condition Condition;
61 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data=NULL) {
return boost::thread(fn,data); }
62 inline void ThreadJoin(Thread& thread) { thread.join(); }
63 inline void ThreadYield() { boost::this_thread::yield(); }
65 #endif //USE_BOOST_THREADS 69 typedef pthread_t Thread;
70 inline Thread ThreadStart(
void* (*fn)(
void*),
void* data=NULL) {
72 pthread_create(&thread,NULL,fn,data);
75 inline void ThreadJoin(Thread& thread) { pthread_join(thread,NULL); }
76 inline void ThreadYield() { pthread_yield(); }
79 Mutex() { mutex = PTHREAD_MUTEX_INITIALIZER; }
80 ~Mutex() { pthread_mutex_destroy(&mutex); }
81 void lock() { pthread_mutex_lock(&mutex); }
82 bool trylock() {
return (pthread_mutex_lock(&mutex) == 0); }
83 void unlock() { pthread_mutex_unlock(&mutex); }
84 pthread_mutex_t mutex;
89 ScopedLock(Mutex& _mutex) :mutex(_mutex) { pthread_mutex_lock(&mutex.mutex); }
90 ~ScopedLock() { pthread_mutex_unlock(&mutex.mutex); }
96 Condition() { cond = PTHREAD_COND_INITIALIZER; }
97 ~Condition() { pthread_cond_destroy(&cond); }
98 void wait(ScopedLock& lock) { pthread_cond_wait(&cond,&lock.mutex.mutex); }
99 void notify_one() { pthread_cond_signal(&cond); }
100 void notify_all() { pthread_cond_broadcast(&cond); }
105 #endif //USE_PTHREADS 108 void ThreadSleep(
double duration);
111 inline void ThreadSleep(
double duration) { usleep(
int(duration*1000000)); }
114 #endif //THREAD_UTILS_H Definition: threadutils.h:94
Definition: threadutils.h:87
Definition: threadutils.h:77