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
7 // Based on the Anthony's idea of scoped_thread in CCiA
9 #ifndef BOOST_THREAD_SCOPED_THREAD_HPP
10 #define BOOST_THREAD_SCOPED_THREAD_HPP
12 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/detail/delete.hpp>
14 #include <boost/thread/detail/move.hpp>
15 #include <boost/thread/thread_functors.hpp>
16 #include <boost/thread/thread_only.hpp>
18 #include <boost/config/abi_prefix.hpp>
24 * RAI @c thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
26 * CallableThread: A callable void(thread&) .
27 * The default is a join_if_joinable.
29 * thread std/boost::thread destructor terminates the program if the thread is not joinable.
30 * Having a wrapper that can join the thread before destroying it seems a natural need.
34 * boost::strict_scoped_thread<> t((boost::thread(F)));
37 template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
38 class strict_scoped_thread
44 BOOST_THREAD_NO_COPYABLE( strict_scoped_thread) /// non copyable
49 #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
50 template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
51 explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
52 t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
55 explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f,
56 typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
57 t_(boost::forward<F>(f)) {}
58 template <class F, class A1>
59 strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
60 t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
61 template <class F, class A1, class A2>
62 strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
63 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
64 template <class F, class A1, class A2, class A3>
65 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) :
66 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
70 * Constructor from the thread to own.
72 * @param t: the thread to own.
74 * Effects: move the thread to own @c t.
76 explicit strict_scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
83 * Effects: Call the CallableThread functor before destroying the owned thread.
84 * Remark: The CallableThread should not throw when joining the thread as the scoped variable is on a scope outside the thread function.
86 ~strict_scoped_thread()
88 CallableThread on_destructor;
96 * RAI @c thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
98 * CallableThread: A callable void(thread&) .
99 * The default is join_if_joinable.
101 * thread std::thread destructor terminates the program if the thread is not joinable.
102 * Having a wrapper that can join the thread before destroying it seems a natural need.
104 * Remark: @c scoped_thread is not a @c thread as @c thread is not designed to be derived from as a polymorphic type.
105 * Anyway @c scoped_thread can be used in most of the contexts a @c thread could be used as it has the
106 * same non-deprecated interface with the exception of the construction.
110 * boost::scoped_thread<> t((boost::thread(F)));
114 template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
121 typedef typename Thread::id id;
122 typedef typename Thread::native_handle_type native_handle_type;
124 BOOST_THREAD_MOVABLE_ONLY( scoped_thread) /// Movable only
127 * Default Constructor.
129 * Effects: wraps a not-a-thread.
131 scoped_thread() BOOST_NOEXCEPT:
140 #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
141 template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
142 explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
143 t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
146 explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f,
147 typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
148 t_(boost::forward<F>(f)) {}
149 template <class F, class A1>
150 scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
151 t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
152 template <class F, class A1, class A2>
153 scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
154 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
155 template <class F, class A1, class A2, class A3>
156 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) :
157 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
161 * Constructor from the thread to own.
163 * @param t: the thread to own.
165 * Effects: move the thread to own @c t.
167 explicit scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
172 // explicit operator Thread()
174 // return boost::move(t_);
180 scoped_thread(BOOST_RV_REF(scoped_thread) x) BOOST_NOEXCEPT :
181 t_(boost::move(BOOST_THREAD_RV(x).t_))
187 * Effects: Call the CallableThread functor before destroying the owned thread.
191 CallableThread on_destructor;
199 scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x)
201 CallableThread on_destructor;
204 t_ = boost::move(BOOST_THREAD_RV(x).t_);
211 void swap(scoped_thread& x) BOOST_NOEXCEPT
216 // forwarded thread functions
217 inline id get_id() const BOOST_NOEXCEPT
232 #ifdef BOOST_THREAD_USES_CHRONO
233 template <class Rep, class Period>
234 bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
236 return t_.try_join_for(rel_time);
239 template <class Clock, class Duration>
240 bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time)
242 return t_.try_join_until(abs_time);
246 native_handle_type native_handle()BOOST_NOEXCEPT
248 return t_.native_handle();
251 bool joinable() const BOOST_NOEXCEPT
253 return t_.joinable();
256 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
262 bool interruption_requested() const BOOST_NOEXCEPT
264 return t_.interruption_requested();
268 static unsigned hardware_concurrency() BOOST_NOEXCEPT
270 return Thread::hardware_concurrency();
273 #ifdef BOOST_THREAD_PROVIDES_PHYSICAL_CONCURRENCY
274 static unsigned physical_concurrency() BOOST_NOEXCEPT
276 return Thread::physical_concurrency();
282 * Effects: swaps the contents of two scoped threads.
284 template <class Destroyer, class Thread >
285 void swap(scoped_thread<Destroyer, Thread>& lhs, scoped_thread<Destroyer, Thread>& rhs)
287 return lhs.swap(rhs);
290 typedef scoped_thread<> joining_thread;
292 #include <boost/config/abi_suffix.hpp>