Thread.h
Go to the documentation of this file.
00001 #ifndef BASELIBITECTURE_BINDING_THREAD_H
00002 #define BASELIBITECTURE_BINDING_THREAD_H
00003 
00023 #if defined(USE_BOOST) and defined (USE_C11)
00024     "ERROR: Inconsistent use of boost and C++11 at the same time"
00025 #endif
00026 
00027 #if !defined(USE_BOOST) and !defined (USE_C11)
00028     "ERROR: You need to have defined either USE_BOOST or USE_C11, have you included the definitions for this package in your build file (eg cmake)?"
00029 #endif
00030 
00031 
00032 
00033 #ifdef USE_BOOST
00034 
00035 #include <boost/thread.hpp>
00036 
00037 #else  // use c++11 std
00038 
00039 #include <thread>
00040 #include <mutex>
00041 #include <chrono>
00042 #include <condition_variable>
00043 
00044 #endif
00045 
00046 
00047 
00048 namespace baselib_binding
00049 {
00050 
00051 #ifdef USE_BOOST
00052 
00053 typedef boost::thread thread;
00054 typedef boost::mutex mutex;
00055 typedef boost::recursive_mutex recursive_mutex;
00056 typedef boost::condition_variable condition_variable;
00057 typedef boost::posix_time::time_duration duration;
00058 
00059 static duration get_duration_secs(double secs)
00060 {
00061     return duration(0,0,secs,0);
00062 }
00063 
00064 // make typedefs of this as follows:
00065 // typedef baselib_binding::unique_lock<CLASS>::type CLASSPtr;
00066 template <class T>
00067 struct unique_lock
00068 {
00069     typedef boost::unique_lock<T> type;
00070 };
00071 
00072 // library-specific all to sleep for secs seconds
00073 #define SLEEP(secs) { boost::this_thread::sleep(boost::posix_time::milliseconds(secs*1000)); }
00074 
00075 // library-specific call to condition variable's timed wait. Returns a boolean whether the wait was successful (no timeout)
00076 #define COND_WAIT(cond_var, lock_guard, timeout) cond_var.timed_wait(lock_guard, baselib_binding::get_duration_secs(timeout))
00077 
00078 // ----------------------------------------------
00079 #else  // use c++11 std
00080 // ----------------------------------------------
00081 
00082 
00083 typedef std::thread thread;
00084 typedef std::mutex mutex;
00085 typedef std::recursive_mutex recursive_mutex;
00086 typedef std::condition_variable condition_variable;
00087 typedef std::chrono::duration<double, std::ratio<1> > duration;
00088 
00089 static duration get_duration_secs(double secs)
00090 {
00091     return duration(secs);
00092 }
00093 
00094 // make typedefs of this as follows:
00095 // typedef baselib_binding::unique_lock<CLASS>::type CLASSPtr;
00096 template <class T>
00097 struct unique_lock
00098 {
00099     typedef std::unique_lock<T> type;
00100 };
00101 
00102 
00103 // library-specific all to sleep for secs seconds
00104 #define SLEEP(secs) \
00105 { \
00106   std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(std::floor(secs*1000))));\
00107 }
00108 
00109 // library-specific call to condition variable's timed wait. Returns a boolean whether the wait was successful (no timeout)
00110 #define COND_WAIT(cond_var, lock_guard, timeout) cond_var.wait_for(lock_guard, baselib_binding::get_duration_secs(timeout)) == std::cv_status::no_timeout
00111 
00112 #endif
00113 
00114 }  // namespace
00115 #endif  // BASELIBITECTURE_BINDING_THREAD_H


baselib_binding
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:38