strusBase  0.17
thread.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_THREAD_HPP_INCLUDED
10 #define _STRUS_THREAD_HPP_INCLUDED
11 
13 #if defined __GNUC__
14 #define STRUS_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
15 #endif
16 
17 #undef STRUS_USE_STD_THREAD
18 #if __cplusplus >= 201103L
19 #if defined __clang__
20 #define STRUS_USE_STD_THREAD
21 #elif defined __GNUC__
22 #if STRUS_GCC_VERSION >= 40900
23 #define STRUS_USE_STD_THREAD
24 #endif // STRUS_GCC_VERSION
25 #endif // __clang__
26 #endif // __cplusplus
27 
28 #ifdef STRUS_USE_STD_THREAD
29 #include <mutex>
30 #include <condition_variable>
31 #include <thread>
32 #include <vector>
33 
34 namespace strus {
35 
36 typedef std::mutex mutex;
38 typedef std::unique_lock<std::mutex> unique_lock;
39 typedef std::unique_lock<std::recursive_mutex> recursive_unique_lock;
40 typedef std::lock_guard<std::mutex> scoped_lock;
42 typedef std::thread thread;
43 
44 class thread_group
45  :public std::vector<std::thread*>
46 {
47  thread_group(){}
48  ~thread_group()
49  {
50  std::vector<std::thread*>::const_iterator ti = begin(), te = end();
51  for (; ti != te; ++ti) delete *ti;
52  }
53  void add_thread( std::thread* thrd)
54  {
55  push_back( thrd);
56  }
57  void join_all()
58  {
59  std::vector<std::thread*>::iterator ti = begin(), te = end();
60  for (; ti != te; ++ti) (*ti)->join();
61  }
62 };
63 
64 class ThreadId
65 {
66 public:
67  typedef std::thread::id Type;
68  static Type get()
69  {
70  return std::this_thread::get_id();
71  }
72 };
73 
74 }//namespace
75 
76 #else //STRUS_USE_STD_THREAD
77 #include <boost/thread/mutex.hpp>
78 #include <boost/thread/recursive_mutex.hpp>
79 #include <boost/thread/condition_variable.hpp>
80 #include <boost/thread.hpp>
81 
82 namespace strus {
83 
87 typedef boost::unique_lock<boost::mutex> unique_lock;
88 typedef boost::unique_lock<boost::recursive_mutex> recursive_unique_lock;
91 
92 class ThreadId
93 {
94 public:
95  typedef boost::thread::id Type;
96  static Type get()
97  {
98  return boost::this_thread::get_id();
99  }
100 };
101 
102 }//namespace
103 
104 #endif //STRUS_USE_STD_THREAD
105 
106 #endif
107 
boost::mutex mutex
Definition: thread.hpp:84
boost::unique_lock< boost::recursive_mutex > recursive_unique_lock
Definition: thread.hpp:88
boost::thread thread
Definition: thread.hpp:90
boost::condition_variable condition_variable
Definition: thread.hpp:89
boost::mutex::scoped_lock scoped_lock
Definition: thread.hpp:86
Definition: thread.hpp:92
boost::unique_lock< boost::mutex > unique_lock
Definition: thread.hpp:87
boost::recursive_mutex recursive_mutex
Definition: thread.hpp:85
boost::thread::id Type
Definition: thread.hpp:95