KrisLibrary  1.0.0
threadutils.h
1 #ifndef THREAD_UTILS_H
2 #define THREAD_UTILS_H
3 
4 #ifndef USE_CPP_THREADS
5 #ifndef USE_BOOST_THREADS
6 #ifndef USE_PTHREADS
7 #ifdef _WIN32
8 #if (_MSC_VER>=1700)
9 #define USE_CPP_THREADS 1
10 #define USE_BOOST_THREADS 0
11 #define USE_PTHREADS 0
12 #else
13 #define USE_CPP_THREADS 0
14 #define USE_BOOST_THREADS 1
15 #define USE_PTHREADS 0
16 #endif
17 #else
18 #define USE_CPP_THREADS 0
19 #define USE_BOOST_THREADS 0
20 #define USE_PTHREADS 1
21 #endif
22 #endif //USE_PTHREADS
23 #endif //USE_BOOST_THREADS
24 #endif //USE_CPP_THREADS
25 
26 
27 #if USE_CPP_THREADS
28 #include <thread>
29 #include <condition_variable>
30 #include <mutex>
31 #include <iostream>
32 typedef std::thread Thread;
33 struct Mutex
34 {
35  Mutex() { }
36  ~Mutex() { mutex.~mutex(); }
37  void lock() { mutex.lock(); }
38  bool trylock() { return (mutex.try_lock() == 0); }
39  void unlock() { mutex.unlock(); }
40  std::mutex mutex;
41 };
42 struct ScopedLock{
43  ScopedLock(Mutex& _mutex) :mutex(_mutex) { mutex.lock(); }
44  ~ScopedLock() { mutex.unlock(); }
45  Mutex& mutex;
46 };
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(); }
51 
52 #endif //USE_CPP_THREADS
53 
54 #if USE_BOOST_THREADS
55 #include <boost/thread.hpp>
56 #include <boost/interprocess/sync/interprocess_condition.hpp>
57 typedef boost::thread Thread;
58 typedef boost::mutex Mutex;
59 typedef boost::mutex::scoped_lock ScopedLock;
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(); }
64 
65 #endif //USE_BOOST_THREADS
66 
67 #if USE_PTHREADS
68 #include <pthread.h>
69 typedef pthread_t Thread;
70 inline Thread ThreadStart(void* (*fn)(void*),void* data=NULL) {
71  pthread_t thread;
72  pthread_create(&thread,NULL,fn,data);
73  return thread;
74 }
75 inline void ThreadJoin(Thread& thread) { pthread_join(thread,NULL); }
76 inline void ThreadYield() { pthread_yield(); }
77 struct Mutex
78 {
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;
85 };
86 
87 struct ScopedLock
88 {
89  ScopedLock(Mutex& _mutex) :mutex(_mutex) { pthread_mutex_lock(&mutex.mutex); }
90  ~ScopedLock() { pthread_mutex_unlock(&mutex.mutex); }
91  Mutex& mutex;
92 };
93 
94 struct Condition
95 {
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); }
101 
102  pthread_cond_t cond;
103 };
104 
105 #endif //USE_PTHREADS
106 
107 #ifdef WIN32
108 void ThreadSleep(double duration);
109 #else
110 #include <unistd.h>
111 inline void ThreadSleep(double duration) { usleep(int(duration*1000000)); }
112 #endif
113 
114 #endif //THREAD_UTILS_H
Definition: threadutils.h:94
Definition: threadutils.h:87
Definition: threadutils.h:77