File indexing completed on 2025-12-15 10:09:31
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_THREAD_SCOPED_THREAD_HPP
0010 #define BOOST_THREAD_SCOPED_THREAD_HPP
0011
0012 #include <boost/thread/detail/config.hpp>
0013 #include <boost/thread/detail/delete.hpp>
0014 #include <boost/thread/detail/move.hpp>
0015 #include <boost/thread/thread_functors.hpp>
0016 #include <boost/thread/thread_only.hpp>
0017 #include <boost/thread/detail/thread_interruption.hpp>
0018
0019 #include <boost/config/abi_prefix.hpp>
0020
0021 namespace boost
0022 {
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
0039 class strict_scoped_thread
0040 {
0041 Thread t_;
0042 struct dummy;
0043 public:
0044
0045 BOOST_THREAD_NO_COPYABLE( strict_scoped_thread)
0046
0047
0048
0049
0050 #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0051 template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
0052 explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
0053 t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
0054 #else
0055 template <class F>
0056 explicit strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f,
0057 typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
0058 t_(boost::forward<F>(f)) {}
0059 template <class F, class A1>
0060 strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
0061 t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
0062 template <class F, class A1, class A2>
0063 strict_scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
0064 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
0065 template <class F, class A1, class A2, class A3>
0066 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) :
0067 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
0068 #endif
0069
0070
0071
0072
0073
0074
0075
0076
0077 explicit strict_scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
0078 t_(boost::move(t))
0079 {
0080 }
0081
0082
0083
0084
0085
0086
0087 ~strict_scoped_thread()
0088 {
0089 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
0090
0091 boost::this_thread::disable_interruption do_not_disturb;
0092 #endif
0093 CallableThread on_destructor;
0094
0095 on_destructor(t_);
0096 }
0097
0098 };
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 template <class CallableThread = join_if_joinable, class Thread=::boost::thread>
0120 class scoped_thread
0121 {
0122 Thread t_;
0123 struct dummy;
0124 public:
0125
0126 typedef typename Thread::id id;
0127 typedef typename Thread::native_handle_type native_handle_type;
0128
0129 BOOST_THREAD_MOVABLE_ONLY( scoped_thread)
0130
0131
0132
0133
0134
0135
0136 scoped_thread() BOOST_NOEXCEPT:
0137 t_()
0138 {
0139 }
0140
0141
0142
0143
0144
0145 #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0146 template <class F, class ...Args, typename = typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type>
0147 explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Args)... args) :
0148 t_(boost::forward<F>(f), boost::forward<Args>(args)...) {}
0149 #else
0150 template <class F>
0151 explicit scoped_thread(BOOST_THREAD_FWD_REF(F) f,
0152 typename disable_if<is_same<typename decay<F>::type, Thread>, void* >::type=0) :
0153 t_(boost::forward<F>(f)) {}
0154 template <class F, class A1>
0155 scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) :
0156 t_(boost::forward<F>(f), boost::forward<A1>(a1)) {}
0157 template <class F, class A1, class A2>
0158 scoped_thread(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) :
0159 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2)) {}
0160 template <class F, class A1, class A2, class A3>
0161 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) :
0162 t_(boost::forward<F>(f), boost::forward<A1>(a1), boost::forward<A2>(a2), boost::forward<A3>(a3)) {}
0163
0164 #endif
0165
0166
0167
0168
0169
0170
0171
0172 explicit scoped_thread(BOOST_THREAD_RV_REF(Thread) t) BOOST_NOEXCEPT :
0173 t_(boost::move(t))
0174 {
0175 }
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 scoped_thread(BOOST_RV_REF(scoped_thread) x) BOOST_NOEXCEPT :
0186 t_(boost::move(BOOST_THREAD_RV(x).t_))
0187 {}
0188
0189
0190
0191
0192
0193
0194 ~scoped_thread()
0195 {
0196 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
0197
0198 boost::this_thread::disable_interruption do_not_disturb;
0199 #endif
0200 CallableThread on_destructor;
0201
0202 on_destructor(t_);
0203 }
0204
0205
0206
0207
0208 scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x)
0209 {
0210 CallableThread on_destructor;
0211
0212 on_destructor(t_);
0213 t_ = boost::move(BOOST_THREAD_RV(x).t_);
0214 return *this;
0215 }
0216
0217
0218
0219
0220 void swap(scoped_thread& x) BOOST_NOEXCEPT
0221 {
0222 t_.swap(x.t_);
0223 }
0224
0225
0226 inline id get_id() const BOOST_NOEXCEPT
0227 {
0228 return t_.get_id();
0229 }
0230
0231 void detach()
0232 {
0233 t_.detach();
0234 }
0235
0236 void join()
0237 {
0238 t_.join();
0239 }
0240
0241 #ifdef BOOST_THREAD_USES_CHRONO
0242 template <class Rep, class Period>
0243 bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
0244 {
0245 return t_.try_join_for(rel_time);
0246 }
0247
0248 template <class Clock, class Duration>
0249 bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time)
0250 {
0251 return t_.try_join_until(abs_time);
0252 }
0253 #endif
0254
0255 native_handle_type native_handle()BOOST_NOEXCEPT
0256 {
0257 return t_.native_handle();
0258 }
0259
0260 bool joinable() const BOOST_NOEXCEPT
0261 {
0262 return t_.joinable();
0263 }
0264
0265 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
0266 void interrupt()
0267 {
0268 t_.interrupt();
0269 }
0270
0271 bool interruption_requested() const BOOST_NOEXCEPT
0272 {
0273 return t_.interruption_requested();
0274 }
0275 #endif
0276
0277 static unsigned hardware_concurrency() BOOST_NOEXCEPT
0278 {
0279 return Thread::hardware_concurrency();
0280 }
0281
0282 #ifdef BOOST_THREAD_PROVIDES_PHYSICAL_CONCURRENCY
0283 static unsigned physical_concurrency() BOOST_NOEXCEPT
0284 {
0285 return Thread::physical_concurrency();
0286 }
0287 #endif
0288 };
0289
0290
0291
0292
0293 template <class Destroyer, class Thread >
0294 void swap(scoped_thread<Destroyer, Thread>& lhs, scoped_thread<Destroyer, Thread>& rhs)
0295 BOOST_NOEXCEPT {
0296 return lhs.swap(rhs);
0297 }
0298
0299 typedef scoped_thread<> joining_thread;
0300 }
0301 #include <boost/config/abi_suffix.hpp>
0302
0303 #endif