scoped_thread.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2009-2012 Anthony Williams
  5. // (C) Copyright 2012 Vicente J. Botet Escriba
  6. // Based on the Anthony's idea of scoped_thread in CCiA
  7. #ifndef BOOST_THREAD_SCOPED_THREAD_HPP
  8. #define BOOST_THREAD_SCOPED_THREAD_HPP
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/detail/delete.hpp>
  11. #include <boost/thread/detail/move.hpp>
  12. #include <boost/thread/thread_functors.hpp>
  13. #include <boost/thread/thread_only.hpp>
  14. #include <boost/thread/detail/thread_interruption.hpp>
  15. #include <boost/config/abi_prefix.hpp>
  16. namespace boost
  17. {
  18. /**
  19. * RAI @c thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
  20. *
  21. * CallableThread: A callable void(thread&) .
  22. * The default is a join_if_joinable.
  23. *
  24. * thread std/boost::thread destructor terminates the program if the thread is not joinable.
  25. * Having a wrapper that can join the thread before destroying it seems a natural need.
  26. *
  27. * Example:
  28. *
  29. * boost::strict_scoped_thread<> t((boost::thread(F)));
  30. *
  31. */
  32. template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
  33. class strict_scoped_thread
  34. {
  35. Thread t_;
  36. struct dummy;
  37. public:
  38. BOOST_THREAD_NO_COPYABLE( strict_scoped_thread) /// non copyable
  39. /*
  40. *
  41. */
  42. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  43. template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
  44. explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
  45. t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
  46. #else
  47. template <class F>
  48. explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f,
  49. typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
  50. t_(boost::forward<F>(f)) {}
  51. template <class F, class A1>
  52. strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
  53. t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
  54. template <class F, class A1, class A2>
  55. strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
  56. t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
  57. template <class F, class A1, class A2, class A3>
  58. strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2, BOOST_THREAD_FWD_REF(A3) a3) :
  59. t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
  60. #endif
  61. /**
  62. * Constructor from the thread to own.
  63. *
  64. * @param t: the thread to own.
  65. *
  66. * Effects: move the thread to own @c t.
  67. */
  68. explicit strict_scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
  69. t_(boost::move(t))
  70. {
  71. }
  72. /**
  73. * Destructor
  74. * Effects: Call the CallableThread functor before destroying the owned thread.
  75. * Remark: The CallableThread should not throw when joining the thread as the scoped variable is on a scope outside the thread function.
  76. */
  77. ~strict_scoped_thread()
  78. {
  79. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  80. // exceptions from a destructor call std::terminate
  81. boost::this_thread::disable_interruption do_not_disturb;
  82. #endif
  83. CallableThread on_destructor;
  84. on_destructor(t_);
  85. }
  86. };
  87. /**
  88. * RAI @c thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
  89. *
  90. * CallableThread: A callable void(thread&) .
  91. * The default is join_if_joinable.
  92. *
  93. * thread std::thread destructor terminates the program if the thread is not joinable.
  94. * Having a wrapper that can join the thread before destroying it seems a natural need.
  95. *
  96. * Remark: @c scoped_thread is not a @c thread as @c thread is not designed to be derived from as a polymorphic type.
  97. * Anyway @c scoped_thread can be used in most of the contexts a @c thread could be used as it has the
  98. * same non-deprecated interface with the exception of the construction.
  99. *
  100. * Example:
  101. *
  102. * boost::scoped_thread<> t((boost::thread(F)));
  103. * t.interrupt();
  104. *
  105. */
  106. template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
  107. class scoped_thread
  108. {
  109. Thread t_;
  110. struct dummy;
  111. public:
  112. typedef typename Thread::id id;
  113. typedef typename Thread::native_handle_type native_handle_type;
  114. BOOST_THREAD_MOVABLE_ONLY( scoped_thread) /// Movable only
  115. /**
  116. * Default Constructor.
  117. *
  118. * Effects: wraps a not-a-thread.
  119. */
  120. scoped_thread() BOOST_NOEXCEPT:
  121. t_()
  122. {
  123. }
  124. /**
  125. *
  126. */
  127. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  128. template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
  129. explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
  130. t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
  131. #else
  132. template <class F>
  133. explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f,
  134. typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
  135. t_(boost::forward<F>(f)) {}
  136. template <class F, class A1>
  137. scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
  138. t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
  139. template <class F, class A1, class A2>
  140. scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
  141. t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
  142. template <class F, class A1, class A2, class A3>
  143. scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2, BOOST_THREAD_FWD_REF(A3) a3) :
  144. t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
  145. #endif
  146. /**
  147. * Constructor from the thread to own.
  148. *
  149. * @param t: the thread to own.
  150. *
  151. * Effects: move the thread to own @c t.
  152. */
  153. explicit scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
  154. t_(boost::move(t))
  155. {
  156. }
  157. // explicit operator Thread()
  158. // {
  159. // return boost::move(t_);
  160. // }
  161. /**
  162. * Move constructor.
  163. */
  164. scoped_thread(BOOST_RV_REF(scoped_thread) x) BOOST_NOEXCEPT :
  165. t_(boost::move(BOOST_THREAD_RV(x).t_))
  166. {}
  167. /**
  168. * Destructor
  169. *
  170. * Effects: Call the CallableThread functor before destroying the owned thread.
  171. */
  172. ~scoped_thread()
  173. {
  174. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  175. // exceptions from a destructor call std::terminate
  176. boost::this_thread::disable_interruption do_not_disturb;
  177. #endif
  178. CallableThread on_destructor;
  179. on_destructor(t_);
  180. }
  181. /**
  182. * Move assignment.
  183. */
  184. scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x)
  185. {
  186. CallableThread on_destructor;
  187. on_destructor(t_);
  188. t_ = boost::move(BOOST_THREAD_RV(x).t_);
  189. return *this;
  190. }
  191. /**
  192. *
  193. */
  194. void swap(scoped_thread& x) BOOST_NOEXCEPT
  195. {
  196. t_.swap(x.t_);
  197. }
  198. // forwarded thread functions
  199. inline id get_id() const BOOST_NOEXCEPT
  200. {
  201. return t_.get_id();
  202. }
  203. void detach()
  204. {
  205. t_.detach();
  206. }
  207. void join()
  208. {
  209. t_.join();
  210. }
  211. #ifdef BOOST_THREAD_USES_CHRONO
  212. template <class Rep, class Period>
  213. bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
  214. {
  215. return t_.try_join_for(rel_time);
  216. }
  217. template <class Clock, class Duration>
  218. bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time)
  219. {
  220. return t_.try_join_until(abs_time);
  221. }
  222. #endif
  223. native_handle_type native_handle()BOOST_NOEXCEPT
  224. {
  225. return t_.native_handle();
  226. }
  227. bool joinable() const BOOST_NOEXCEPT
  228. {
  229. return t_.joinable();
  230. }
  231. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  232. void interrupt()
  233. {
  234. t_.interrupt();
  235. }
  236. bool interruption_requested() const BOOST_NOEXCEPT
  237. {
  238. return t_.interruption_requested();
  239. }
  240. #endif
  241. static unsigned hardware_concurrency() BOOST_NOEXCEPT
  242. {
  243. return Thread::hardware_concurrency();
  244. }
  245. #ifdef BOOST_THREAD_PROVIDES_PHYSICAL_CONCURRENCY
  246. static unsigned physical_concurrency() BOOST_NOEXCEPT
  247. {
  248. return Thread::physical_concurrency();
  249. }
  250. #endif
  251. };
  252. /**
  253. * Effects: swaps the contents of two scoped threads.
  254. */
  255. template <class Destroyer, class Thread >
  256. void swap(scoped_thread<Destroyer, Thread>& lhs, scoped_thread<Destroyer, Thread>& rhs)
  257. BOOST_NOEXCEPT {
  258. return lhs.swap(rhs);
  259. }
  260. typedef scoped_thread<> joining_thread;
  261. }
  262. #include <boost/config/abi_suffix.hpp>
  263. #endif