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 typedef std::mutex Mutex;
34 //no std::scoped_lock until c++17
35 struct ScopedLock{
36  explicit ScopedLock(Mutex& _mutex) :mutex(_mutex) { mutex.lock(); }
37  ~ScopedLock() { mutex.unlock(); }
38  Mutex& mutex;
39 };
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(); }
44 
45 #endif //USE_CPP_THREADS
46 
47 #if USE_BOOST_THREADS
48 #include <boost/thread.hpp>
49 #include <boost/interprocess/sync/interprocess_condition.hpp>
50 typedef boost::thread Thread;
51 typedef boost::mutex Mutex;
52 typedef boost::mutex::scoped_lock ScopedLock;
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(); }
57 
58 #endif //USE_BOOST_THREADS
59 
60 #if USE_PTHREADS
61 #include <pthread.h>
62 typedef pthread_t Thread;
63 inline Thread ThreadStart(void* (*fn)(void*),void* data=NULL) {
64  pthread_t thread;
65  pthread_create(&thread,NULL,fn,data);
66  return thread;
67 }
68 inline void ThreadJoin(Thread& thread) { pthread_join(thread,NULL); }
69 inline void ThreadYield() { pthread_yield(); }
70 struct Mutex
71 {
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;
78 };
79 
80 struct ScopedLock
81 {
82  ScopedLock(Mutex& _mutex) :mutex(_mutex) { pthread_mutex_lock(&mutex.mutex); }
83  ~ScopedLock() { pthread_mutex_unlock(&mutex.mutex); }
84  Mutex& mutex;
85 };
86 
87 struct Condition
88 {
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); }
94 
95  pthread_cond_t cond;
96 };
97 
98 #endif //USE_PTHREADS
99 
100 #ifdef WIN32
101 void ThreadSleep(double duration);
102 #else
103 #include <unistd.h>
104 inline void ThreadSleep(double duration) { usleep(int(duration*1000000)); }
105 #endif
106 
107 #endif //THREAD_UTILS_H
Definition: threadutils.h:87
Definition: threadutils.h:80
Definition: threadutils.h:70