thread_ref.qbk 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. [/
  2. (C) Copyright 2007-8 Anthony Williams.
  3. (C) Copyright 2011-12 Vicente J. Botet Escriba.
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt).
  7. ]
  8. [section:thread_management Thread Management]
  9. [section:synopsis Synopsis]
  10. #include <boost/thread/thread.hpp>
  11. namespace boost
  12. {
  13. class thread;
  14. void swap(thread& lhs,thread& rhs) noexcept;
  15. namespace this_thread
  16. {
  17. thread::id get_id() noexcept;
  18. template<typename TimeDuration>
  19. void yield() noexcept;
  20. template <class Clock, class Duration>
  21. void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
  22. template <class Rep, class Period>
  23. void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  24. namespace no_interruption_point // EXTENSION
  25. {
  26. template <class Clock, class Duration>
  27. void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
  28. template <class Rep, class Period>
  29. void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  30. }
  31. template<typename Callable>
  32. void at_thread_exit(Callable func); // EXTENSION
  33. void interruption_point(); // EXTENSION
  34. bool interruption_requested() noexcept; // EXTENSION
  35. bool interruption_enabled() noexcept; // EXTENSION
  36. class disable_interruption; // EXTENSION
  37. class restore_interruption; // EXTENSION
  38. #if defined BOOST_THREAD_USES_DATETIME
  39. template <TimeDuration>
  40. void sleep(TimeDuration const& rel_time); // DEPRECATED
  41. void sleep(system_time const& abs_time); // DEPRECATED
  42. #endif
  43. }
  44. class thread_group; // EXTENSION
  45. }
  46. [endsect] [/section:synopsis Synopsis]
  47. [section:tutorial Tutorial]
  48. The __thread__ class is responsible for launching and managing threads. Each __thread__ object represents a single thread of execution,
  49. or __not_a_thread__, and at most one __thread__ object represents a given thread of execution: objects of type __thread__ are not
  50. copyable.
  51. Objects of type __thread__ are movable, however, so they can be stored in move-aware containers, and returned from functions. This
  52. allows the details of thread creation to be wrapped in a function.
  53. boost::thread make_thread();
  54. void f()
  55. {
  56. boost::thread some_thread=make_thread();
  57. some_thread.join();
  58. }
  59. [note On compilers that support rvalue references, __thread__ provides a proper move constructor and move-assignment operator, and
  60. therefore meets the C++0x ['MoveConstructible] and ['MoveAssignable] concepts. With such compilers, __thread__ can therefore be used
  61. with containers that support those concepts.
  62. For other compilers, move support is provided with a move emulation layer, so containers must explicitly detect that move emulation
  63. layer. See <boost/thread/detail/move.hpp> for details.]
  64. [section:launching Launching threads]
  65. A new thread is launched by passing an object of a callable type that can be invoked with no parameters to the constructor. The
  66. object is then copied into internal storage, and invoked on the newly-created thread of execution. If the object must not (or
  67. cannot) be copied, then `boost::ref` can be used to pass in a reference to the function object. In this case, the user of
  68. __boost_thread__ must ensure that the referred-to object outlives the newly-created thread of execution.
  69. struct callable
  70. {
  71. void operator()();
  72. };
  73. boost::thread copies_are_safe()
  74. {
  75. callable x;
  76. return boost::thread(x);
  77. } // x is destroyed, but the newly-created thread has a copy, so this is OK
  78. boost::thread oops()
  79. {
  80. callable x;
  81. return boost::thread(boost::ref(x));
  82. } // x is destroyed, but the newly-created thread still has a reference
  83. // this leads to undefined behaviour
  84. If you wish to construct an instance of __thread__ with a function or callable object that requires arguments to be supplied,
  85. this can be done by passing additional arguments to the __thread__ constructor:
  86. void find_the_question(int the_answer);
  87. boost::thread deep_thought_2(find_the_question,42);
  88. The arguments are ['copied] into the internal thread structure: if a reference is required, use `boost::ref`, just as for references
  89. to callable functions.
  90. There is an unspecified limit on the number of additional arguments that can be passed.
  91. [endsect]
  92. [section:attributes Thread attributes]
  93. Thread launched in this way are created with implementation defined thread attributes as stack size, scheduling,
  94. priority, ... or any platform specific attributes. It is not evident how to provide a portable interface that allows
  95. the user to set the platform specific attributes. Boost.Thread stay in the middle road through the class
  96. thread::attributes which allows to set at least in a portable way the stack size as follows:
  97. boost::thread::attributes attrs;
  98. attrs.set_stack_size(4096*10);
  99. boost::thread deep_thought_2(attrs, find_the_question, 42);
  100. Even for this simple attribute there could be portable issues as some platforms could require that the stack size
  101. should have a minimal size and/or be a multiple of a given page size.
  102. The library adapts the requested size to the platform constraints so that the user doesn't need to take care of it.
  103. This is the single attribute that is provided in a portable way. In order to set any other thread attribute at
  104. construction time the user needs to use non portable code.
  105. On PThread platforms the user will need to get the thread attributes handle and use it for whatever attribute.
  106. Next follows how the user could set the stack size and the scheduling policy on PThread platforms.
  107. boost::thread::attributes attrs;
  108. // set portable attributes
  109. // ...
  110. attr.set_stack_size(4096*10);
  111. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  112. // ... window version
  113. #elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
  114. // ... pthread version
  115. pthread_attr_setschedpolicy(attr.native_handle(), SCHED_RR);
  116. #else
  117. #error "Boost threads unavailable on this platform"
  118. #endif
  119. boost::thread th(attrs, find_the_question, 42);
  120. On Windows platforms it is not so simple as there is no type that compiles the thread attributes.
  121. There is a linked to the creation of a thread on Windows that is emulated via the thread::attributes class. This is the LPSECURITY_ATTRIBUTES lpThreadAttributes.
  122. Boost.Thread provides a non portable set_security function so that the user can provide it before the thread creation as follows
  123. [/Boost.Thread creates Windows threads that are suspended. Then it calls to the virtual function set_attributes and last it resumes the thread.
  124. The user needs to define a class that inherits from the class thread::attributes that defines a virtual function set_attributes to set any specific Windows thread attribute.
  125. class MyWinTthreadAttributes : boost::thread::attributes
  126. {
  127. public:
  128. void set_attributes(boost::thread::native_handle_type h)
  129. {
  130. // use any specific windows thread setting
  131. }
  132. };
  133. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  134. MyWinTthreadAttributes attrs;
  135. // set portable attributes
  136. // ...
  137. attr.set_stack_size(4096*10);
  138. boost::thread th(attrs, find_the_question, 42);
  139. #else
  140. #error "Platform not supported"
  141. #endif
  142. ]
  143. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  144. boost::thread::attributes attrs;
  145. // set portable attributes
  146. attr.set_stack_size(4096*10);
  147. // set non portable attribute
  148. LPSECURITY_ATTRIBUTES sec;
  149. // init sec
  150. attr.set_security(sec);
  151. boost::thread th(attrs, find_the_question, 42);
  152. // Set other thread attributes using the native_handle_type.
  153. //...
  154. #else
  155. #error "Platform not supported"
  156. #endif
  157. [endsect]
  158. [section:exceptions Exceptions in thread functions]
  159. If the function or callable object passed to the __thread__ constructor propagates an exception when invoked that is not of type
  160. __thread_interrupted__, `std::terminate()` is called.
  161. [endsect]
  162. [section:detach Detaching thread]
  163. A thread can be detached by explicitly invoking the __detach__ member function on the __thread__
  164. object. In this case, the __thread__ object ceases to represent the now-detached thread, and instead represents __not_a_thread__.
  165. int main()
  166. {
  167. boost::thread t(my_func);
  168. t.detach();
  169. }
  170. [endsect]
  171. [section:join Joining a thread]
  172. In order to wait for a thread of execution to finish, the __join__, __join_for or __join_until (__timed_join__ deprecated) member functions of the __thread__ object must be
  173. used. __join__ will block the calling thread until the thread represented by the __thread__ object has completed.
  174. int main()
  175. {
  176. boost::thread t(my_func);
  177. t.join();
  178. }
  179. If the thread of
  180. execution represented by the __thread__ object has already completed, or the __thread__ object represents __not_a_thread__, then __join__
  181. returns immediately.
  182. int main()
  183. {
  184. boost::thread t;
  185. t.join(); // do nothing
  186. }
  187. Timed based join are similar, except that a call to __join_for or __join_until will also return if the thread being waited for
  188. does not complete when the specified time has elapsed or reached respectively.
  189. int main()
  190. {
  191. boost::thread t;
  192. if ( t.join_for(boost::chrono::milliseconds(500)) )
  193. // do something else
  194. t.join(); // join anyway
  195. }
  196. [endsect]
  197. [section:destructor1 Destructor V1-2]
  198. When the __thread__ object that represents a thread of execution is destroyed the thread becomes ['detached]. Once a thread is
  199. detached, it will continue executing until the invocation of the function or callable object supplied on construction has completed,
  200. or the program is terminated. A thread can also be detached by explicitly invoking the __detach__ member function on the __thread__
  201. object. In this case, the __thread__ object ceases to represent the now-detached thread, and instead represents __not_a_thread__.
  202. [endsect]
  203. [section:destructor2 Destructor V3-X]
  204. When the __thread__ object that represents a thread of execution is destroyed the program terminates if the thread is __joinable__.
  205. int main()
  206. {
  207. boost::thread t(my_func);
  208. } // calls std::terminate()
  209. You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
  210. int main()
  211. {
  212. boost::thread t(my_func);
  213. boost::thread_joiner g(t);
  214. // do something else
  215. } // here the thread_joiner destructor will join the thread before it is destroyed.
  216. [endsect]
  217. [section:interruption Interruption]
  218. A running thread can be ['interrupted] by invoking the __interrupt__ member function of the corresponding __thread__ object. When the
  219. interrupted thread next executes one of the specified __interruption_points__ (or if it is currently __blocked__ whilst executing one)
  220. with interruption enabled, then a __thread_interrupted__ exception will be thrown in the interrupted thread. Unless this exception is
  221. caught inside the interrupted thread's thread-main function, the stack unwinding process (as with any other exception) causes the
  222. destructors with automatic storage duration to be executed. Unlike other exceptions, when __thread_interrupted__ is propagated out of
  223. thread-main function, this does not cause the call to `std::terminate`; the effect is as though the thread-main function has returned
  224. normally.
  225. If a thread wishes to avoid being interrupted, it can create an instance of __disable_interruption__. Objects of this class disable
  226. interruption for the thread that created them on construction, and restore the interruption state to whatever it was before on
  227. destruction:
  228. void f()
  229. {
  230. // interruption enabled here
  231. {
  232. boost::this_thread::disable_interruption di;
  233. // interruption disabled
  234. {
  235. boost::this_thread::disable_interruption di2;
  236. // interruption still disabled
  237. } // di2 destroyed, interruption state restored
  238. // interruption still disabled
  239. } // di destroyed, interruption state restored
  240. // interruption now enabled
  241. }
  242. The effects of an instance of __disable_interruption__ can be temporarily reversed by constructing an instance of
  243. __restore_interruption__, passing in the __disable_interruption__ object in question. This will
  244. restore the interruption state to what it was when the __disable_interruption__ object was constructed, and then
  245. disable interruption again when the __restore_interruption__ object is destroyed.
  246. void g()
  247. {
  248. // interruption enabled here
  249. {
  250. boost::this_thread::disable_interruption di;
  251. // interruption disabled
  252. {
  253. boost::this_thread::restore_interruption ri(di);
  254. // interruption now enabled
  255. } // ri destroyed, interruption disable again
  256. } // di destroyed, interruption state restored
  257. // interruption now enabled
  258. }
  259. At any point, the interruption state for the current thread can be queried by calling __interruption_enabled__.
  260. [#interruption_points]
  261. [heading Predefined Interruption Points]
  262. The following functions are ['interruption points], which will throw __thread_interrupted__ if interruption is enabled for the
  263. current thread, and interruption is requested for the current thread:
  264. * [join_link `boost::thread::join()`]
  265. * [timed_join_link `boost::thread::timed_join()`]
  266. * `boost::__thread::__try_join_for()`,
  267. * `boost::__thread::__try_join_until()`,
  268. * [cond_wait_link `boost::condition_variable::wait()`]
  269. * [cond_timed_wait_link `boost::condition_variable::timed_wait()`]
  270. * `boost::__condition_variable::__wait_for()`
  271. * `boost::__condition_variable::__wait_until()`
  272. * [cond_any_wait_link `boost::condition_variable_any::wait()`]
  273. * [cond_any_timed_wait_link `boost::condition_variable_any::timed_wait()`]
  274. * `boost::__condition_variable_any::__cvany_wait_for()`
  275. * `boost::__condition_variable_any::__cvany_wait_until()`
  276. * [link thread.thread_management.thread.sleep `boost::thread::sleep()`]
  277. * `boost::this_thread::__sleep_for()`
  278. * `boost::this_thread::__sleep_until()`
  279. * __interruption_point__
  280. [endsect]
  281. [section:id Thread IDs]
  282. Objects of class __thread_id__ can be used to identify threads. Each running thread of execution has a unique ID obtainable
  283. from the corresponding __thread__ by calling the `get_id()` member function, or by calling `boost::this_thread::get_id()` from
  284. within the thread. Objects of class __thread_id__ can be copied, and used as keys in associative containers: the full range of
  285. comparison operators is provided. Thread IDs can also be written to an output stream using the stream insertion operator, though the
  286. output format is unspecified.
  287. Each instance of __thread_id__ either refers to some thread, or __not_a_thread__. Instances that refer to __not_a_thread__
  288. compare equal to each other, but not equal to any instances that refer to an actual thread of execution. The comparison operators on
  289. __thread_id__ yield a total order for every non-equal thread ID.
  290. [endsect]
  291. [section:native_in Using native interfaces with Boost.Thread resources]
  292. __thread__ class has members `native_handle_type` and `native_handle` providing access to the underlying native handle.
  293. This native handle can be used to change for example the scheduling.
  294. In general, it is not safe to use this handle with operations that can conflict with the ones provided by Boost.Thread. An example of bad usage could be detaching a thread directly as it will not change the internals of the __thread__ instance, so for example the joinable function will continue to return true, while the native thread is no more joinable.
  295. thread t(fct);
  296. thread::native_handle_type hnd=t.native_handle();
  297. pthread_detach(hnd);
  298. assert(t.joinable());
  299. [endsect]
  300. [section:native_from Using Boost.Thread interfaces in a native thread]
  301. Any thread of execution created using the native interface is called a native thread in this documentation.
  302. The first example of a native thread of execution is the main thread.
  303. The user can access to some synchronization functions related to the native current thread using the `boost::this_thread` `yield`, `sleep`, __sleep_for, __sleep_until, functions.
  304. int main() {
  305. // ...
  306. boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
  307. // ...
  308. }
  309. Of course all the synchronization facilities provided by Boost.Thread are also available on native threads.
  310. The `boost::this_thread` interrupt related functions behave in a degraded mode when called from a thread created using the native interface, i.e. `boost::this_thread::interruption_enabled()` returns false. As consequence the use of `boost::this_thread::disable_interruption` and `boost::this_thread::restore_interruption` will do nothing and calls to `boost::this_thread::interruption_point()` will be just ignored.
  311. As the single way to interrupt a thread is through a __thread__ instance, `interruption_request()` will return false for the native threads.
  312. [heading `pthread_exit` POSIX limitation]
  313. `pthread_exit` in glibc/NPTL causes a "forced unwind" that is almost like a C++ exception, but not quite. On Mac OS X, for example, `pthread_exit` unwinds without calling C++ destructors.
  314. This behavior is incompatible with the current Boost.Thread design, so the use of this function in a POSIX thread result in undefined behavior of any Boost.Thread function.
  315. [endsect]
  316. [endsect] [/section:tutorial Tutorial]
  317. [section:thread Class `thread`]
  318. #include <boost/thread/thread.hpp>
  319. class thread
  320. {
  321. public:
  322. class attributes; // EXTENSION
  323. thread() noexcept;
  324. ~thread();
  325. thread(const thread&) = delete;
  326. thread& operator=(const thread&) = delete;
  327. // move support
  328. thread(thread&&) noexcept;
  329. thread& operator=(thread&&) noexcept;
  330. template <class F>
  331. explicit thread(F f);
  332. template <class F>
  333. thread(F &&f);
  334. template <class F,class A1,class A2,...>
  335. thread(F f,A1 a1,A2 a2,...);
  336. template <class F, class ...Args>
  337. explicit thread(F&& f, Args&&... args);
  338. template <class F>
  339. explicit thread(attributes& attrs, F f); // EXTENSION
  340. template <class F>
  341. thread(attributes& attrs, F &&f); // EXTENSION
  342. template <class F, class ...Args>
  343. explicit thread(attributes& attrs, F&& f, Args&&... args);
  344. void swap(thread& x) noexcept;
  345. class id;
  346. id get_id() const noexcept;
  347. bool joinable() const noexcept;
  348. void join();
  349. template <class Rep, class Period>
  350. bool try_join_for(const chrono::duration<Rep, Period>& rel_time); // EXTENSION
  351. template <class Clock, class Duration>
  352. bool try_join_until(const chrono::time_point<Clock, Duration>& t); // EXTENSION
  353. void detach();
  354. static unsigned hardware_concurrency() noexcept;
  355. static unsigned physical_concurrency() noexcept;
  356. typedef platform-specific-type native_handle_type;
  357. native_handle_type native_handle();
  358. void interrupt(); // EXTENSION
  359. bool interruption_requested() const noexcept; // EXTENSION
  360. #if defined BOOST_THREAD_USES_DATETIME
  361. bool timed_join(const system_time& wait_until); // DEPRECATED
  362. template<typename TimeDuration>
  363. bool timed_join(TimeDuration const& rel_time); // DEPRECATED
  364. static void sleep(const system_time& xt);// DEPRECATED
  365. #endif
  366. #if defined BOOST_THREAD_PROVIDES_THREAD_EQ
  367. bool operator==(const thread& other) const; // DEPRECATED
  368. bool operator!=(const thread& other) const; // DEPRECATED
  369. #endif
  370. static void yield() noexcept; // DEPRECATED
  371. };
  372. void swap(thread& lhs,thread& rhs) noexcept;
  373. [section:default_constructor Default Constructor]
  374. thread() noexcept;
  375. [variablelist
  376. [[Effects:] [Constructs a __thread__ instance that refers to __not_a_thread__.]]
  377. [[Postconditions:] [`this->get_id()==thread::id()`]]
  378. [[Throws:] [Nothing]]
  379. ]
  380. [endsect]
  381. [section:move_constructor Move Constructor]
  382. thread(thread&& other) noexcept;
  383. [variablelist
  384. [[Effects:] [Transfers ownership of the thread managed by `other` (if any) to the newly constructed __thread__ instance.]]
  385. [[Postconditions:] [`other.get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the construction]]
  386. [[Throws:] [Nothing]]
  387. ]
  388. [endsect]
  389. [section:move_assignment Move assignment operator]
  390. thread& operator=(thread&& other) noexcept;
  391. [variablelist
  392. [[Effects:] [Transfers ownership of the thread managed by `other` (if
  393. any) to `*this`.
  394. - if defined BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE: If the thread is joinable call __detach__, DEPRECATED
  395. - if defined BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE: If the thread is joinable calls to `std::terminate()`.
  396. ]]
  397. [[Postconditions:] [`other->get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the assignment.]]
  398. [[Throws:] [Nothing]]
  399. ]
  400. [endsect]
  401. [section:callable_constructor Thread Constructor]
  402. template<typename Callable>
  403. thread(Callable func);
  404. [variablelist
  405. [[Requires:] [`Callable` must be Copyable and `func()` must be a valid expression.]]
  406. [[Effects:] [`func` is copied into storage managed internally by the thread library, and that copy is invoked on a newly-created
  407. thread of execution. If this invocation results in an exception being propagated into the internals of the thread library that is
  408. not of type __thread_interrupted__, then `std::terminate()` will be called. Any return value from this invocation is ignored.]]
  409. [[Postconditions:] [`*this` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]
  410. [[Throws:] [__thread_resource_error__ if an error occurs. ]]
  411. [[Error Conditions:] [
  412. [*resource_unavailable_try_again] : the system lacked the necessary resources to create an- other thread, or the system-imposed limit on the number of threads in a process would be exceeded.
  413. ]]
  414. ]
  415. [endsect]
  416. [section:attr_callable_constructor Thread Attributes Constructor EXTENSION]
  417. template<typename Callable>
  418. thread(attributes& attrs, Callable func);
  419. [variablelist
  420. [[Preconditions:] [`Callable` must be copyable.]]
  421. [[Effects:] [`func` is copied into storage managed internally by the thread library, and that copy is invoked on a newly-created
  422. thread of execution with the specified attributes. If this invocation results in an exception being propagated into the internals of the thread library that is
  423. not of type __thread_interrupted__, then `std::terminate()` will be called. Any return value from this invocation is ignored.
  424. If the attributes declare the native thread as detached, the boost::thread will be detached.]]
  425. [[Postconditions:] [`*this` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]
  426. [[Throws:] [__thread_resource_error__ if an error occurs. ]]
  427. [[Error Conditions:] [
  428. [*resource_unavailable_try_again] : the system lacked the necessary resources to create an- other thread, or the system-imposed limit on the number of threads in a process would be exceeded.
  429. ]]
  430. ]
  431. [endsect]
  432. [section:callable_move_constructor Thread Callable Move Constructor]
  433. template<typename Callable>
  434. thread(Callable &&func);
  435. [variablelist
  436. [[Preconditions:] [`Callable` must be Movable.]]
  437. [[Effects:] [`func` is moved into storage managed internally by the thread library, and that copy is invoked on a newly-created
  438. thread of execution. If this invocation results in an exception being propagated into the internals of the thread library that is
  439. not of type __thread_interrupted__, then `std::terminate()` will be called. Any return value from this invocation is ignored.]]
  440. [[Postconditions:] [`*this` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]
  441. [[Throws:] [__thread_resource_error__ if an error occurs. ]]
  442. [[Error Conditions:] [
  443. [*resource_unavailable_try_again] : the system lacked the necessary resources to create an- other thread, or the system-imposed limit on the number of threads in a process would be exceeded.
  444. ]]
  445. ]
  446. [endsect]
  447. [section:attr_callable_move_constructor Thread Attributes Move Constructor EXTENSION]
  448. template<typename Callable>
  449. thread(attributes& attrs, Callable func);
  450. [variablelist
  451. [[Preconditions:] [`Callable` must be copyable.]]
  452. [[Effects:] [`func` is copied into storage managed internally by the thread library, and that copy is invoked on a newly-created
  453. thread of execution with the specified attributes. If this invocation results in an exception being propagated into the internals of the thread library that is
  454. not of type __thread_interrupted__, then `std::terminate()` will be called. Any return value from this invocation is ignored.
  455. If the attributes declare the native thread as detached, the boost::thread will be detached.]]
  456. [[Postconditions:] [`*this` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]
  457. [[Throws:] [__thread_resource_error__ if an error occurs. ]]
  458. [[Error Conditions:] [
  459. [*resource_unavailable_try_again] : the system lacked the necessary resources to create an- other thread, or the system-imposed limit on the number of threads in a process would be exceeded.
  460. ]]
  461. ]
  462. [endsect]
  463. [section:multiple_argument_constructor Thread Constructor with arguments]
  464. template <class F,class A1,class A2,...>
  465. thread(F f,A1 a1,A2 a2,...);
  466. [variablelist
  467. [[Preconditions:] [`F` and each `A`n must be copyable or movable.]]
  468. [[Effects:] [As if [link
  469. thread.thread_management.thread.callable_constructor
  470. `thread(boost::bind(f,a1,a2,...))`. Consequently, `f` and each `a`n
  471. are copied into internal storage for access by the new thread.]]]
  472. [[Postconditions:] [`*this` refers to the newly created thread of execution.]]
  473. [[Throws:] [__thread_resource_error__ if an error occurs.]]
  474. [[Error Conditions:] [
  475. [*resource_unavailable_try_again] : the system lacked the necessary resources to create an- other thread, or the system-imposed limit on the number of threads in a process would be exceeded.
  476. ]]
  477. [[Note:] [Currently up to nine additional arguments `a1` to `a9` can be specified in addition to the function `f`.]]
  478. ]
  479. [endsect]
  480. [section:destructor Thread Destructor]
  481. ~thread();
  482. [variablelist
  483. [[Effects:] [
  484. - if defined BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE: If the thread is joinable calls __detach__, DEPRECATED
  485. - if defined BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE: If the thread is joinable calls to `std::terminate`. Destroys `*this`.]]
  486. [[Throws:] [Nothing.]]
  487. [[Note:] [The reason to moving to std::terminate is that either implicitly detaching or joining a `joinable()` thread in its destructor could result in difficult to debug correctness (for `detach`) or performance (for `join`) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. Join the thread before destroying or use a scoped thread.]]
  488. ]
  489. [endsect]
  490. [section:joinable Member function `joinable()`]
  491. bool joinable() const noexcept;
  492. [variablelist
  493. [[Returns:] [`true` if `*this` refers to a thread of execution, `false` otherwise.]]
  494. [[Throws:] [Nothing]]
  495. ]
  496. [endsect]
  497. [section:join Member function `join()`]
  498. void join();
  499. [variablelist
  500. [[Preconditions:] [the thread is joinable.]]
  501. [[Effects:] [If `*this` refers to a thread of execution, waits for that thread of execution to complete.]]
  502. [[Synchronization:] [The completion of the thread represented by `*this` synchronizes with the
  503. corresponding successful `join()` return. ]]
  504. [[Note:] [Operations on *this are not synchronized.
  505. ]]
  506. [[Postconditions:] [If `*this` refers to a thread of execution on entry, that thread of execution has completed. `*this` no longer refers to any thread of execution.]]
  507. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted or `system_error`]]
  508. [[Error Conditions:] [
  509. [*resource_deadlock_would_occur]: if deadlock is detected or `this->get_id() == boost::this_thread::get_id()`.
  510. [*invalid_argument]: if the thread is not joinable and `BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED` is defined.
  511. [/
  512. [*no_such_process]: if the thread is not valid.
  513. ]
  514. ]]
  515. [[Notes:] [`join()` is one of the predefined __interruption_points__.]]
  516. ]
  517. [endsect]
  518. [section:timed_join Member function `timed_join()` DEPRECATED]
  519. bool timed_join(const system_time& wait_until);
  520. template<typename TimeDuration>
  521. bool timed_join(TimeDuration const& rel_time);
  522. [warning
  523. DEPRECATED since 3.00.
  524. Use instead __try_join_for, __try_join_until.
  525. ]
  526. [variablelist
  527. [[Preconditions:] [the thread is joinable.]]
  528. [[Effects:] [If `*this` refers to a thread of execution, waits for that thread of execution to complete, the time `wait_until` has
  529. been reach or the specified duration `rel_time` has elapsed. If `*this` doesn't refer to a thread of execution, returns immediately.]]
  530. [[Returns:] [`true` if `*this` refers to a thread of execution on entry, and that thread of execution has completed before the call
  531. times out, `false` otherwise.]]
  532. [[Postconditions:] [If `*this` refers to a thread of execution on entry, and `timed_join` returns `true`, that thread of execution
  533. has completed, and `*this` no longer refers to any thread of execution. If this call to `timed_join` returns `false`, `*this` is
  534. unchanged.]]
  535. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted or `system_error`]]
  536. [[Error Conditions:] [
  537. [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
  538. [*invalid_argument]: if the thread is not joinable and BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined.
  539. [/
  540. [*no_such_process]: if the thread is not valid.
  541. ]
  542. ]]
  543. [[Notes:] [`timed_join()` is one of the predefined __interruption_points__.]]
  544. ]
  545. [endsect]
  546. [section:try_join_for Member function `try_join_for()` EXTENSION]
  547. template <class Rep, class Period>
  548. bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
  549. [variablelist
  550. [[Preconditions:] [the thread is joinable.]]
  551. [[Effects:] [If `*this` refers to a thread of execution, waits for that thread of execution to complete,
  552. the specified duration `rel_time` has elapsed. If `*this` doesn't refer to a thread of execution, returns immediately.]]
  553. [[Returns:] [`true` if `*this` refers to a thread of execution on entry, and that thread of execution has completed before the call
  554. times out, `false` otherwise.]]
  555. [[Postconditions:] [If `*this` refers to a thread of execution on entry, and `try_join_for` returns `true`, that thread of execution
  556. has completed, and `*this` no longer refers to any thread of execution. If this call to `try_join_for` returns `false`, `*this` is
  557. unchanged.]]
  558. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted or `system_error`]]
  559. [[Error Conditions:] [
  560. [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
  561. [*invalid_argument]: if the thread is not joinable and BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined.
  562. [/
  563. [*no_such_process]: if the thread is not valid.
  564. ]
  565. ]]
  566. [[Notes:] [`try_join_for()` is one of the predefined __interruption_points__.]]
  567. ]
  568. [endsect]
  569. [section:try_join_until Member function `try_join_until()` EXTENSION]
  570. template <class Clock, class Duration>
  571. bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time);
  572. [variablelist
  573. [[Preconditions:] [the thread is joinable.]]
  574. [[Effects:] [If `*this` refers to a thread of execution, waits for that thread of execution to complete, the time `abs_time` has
  575. been reach. If `*this` doesn't refer to a thread of execution, returns immediately.]]
  576. [[Returns:] [`true` if `*this` refers to a thread of execution on entry, and that thread of execution has completed before the call
  577. times out, `false` otherwise.]]
  578. [[Postconditions:] [If `*this` refers to a thread of execution on entry, and `try_join_until` returns `true`, that thread of execution
  579. has completed, and `*this` no longer refers to any thread of execution. If this call to `try_join_until` returns `false`, `*this` is
  580. unchanged.]]
  581. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted or `system_error`]]
  582. [[Error Conditions:] [
  583. [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
  584. [*invalid_argument]: if the thread is not joinable and BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined.
  585. [/
  586. [*no_such_process]: if the thread is not valid.
  587. ]
  588. ]]
  589. [[Notes:] [`try_join_until()` is one of the predefined __interruption_points__.]]
  590. ]
  591. [endsect]
  592. [section:detach Member function `detach()`]
  593. void detach();
  594. [variablelist
  595. [[Preconditions:] [the thread is joinable.]]
  596. [[Effects:] [The thread of execution becomes detached, and no longer has an associated __thread__ object.]]
  597. [[Postconditions:] [`*this` no longer refers to any thread of execution.]]
  598. [[Throws:] [`system_error`]]
  599. [[Error Conditions:] [
  600. [*no_such_process]: if the thread is not valid.
  601. [*invalid_argument]: if the thread is not joinable and BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined.
  602. ]]
  603. ]
  604. [endsect]
  605. [section:get_id Member function `get_id()`]
  606. thread::id get_id() const noexcept;
  607. [variablelist
  608. [[Returns:] [If `*this` refers to a thread of execution, an instance of __thread_id__ that represents that thread. Otherwise returns
  609. a default-constructed __thread_id__.]]
  610. [[Throws:] [Nothing]]
  611. ]
  612. [endsect]
  613. [section:interrupt Member function `interrupt()` EXTENSION]
  614. void interrupt();
  615. [variablelist
  616. [[Effects:] [If `*this` refers to a thread of execution, request that the thread will be interrupted the next time it enters one of
  617. the predefined __interruption_points__ with interruption enabled, or if it is currently __blocked__ in a call to one of the
  618. predefined __interruption_points__ with interruption enabled. Otherwise do noting.]]
  619. [[Throws:] [Nothing]]
  620. ]
  621. [endsect]
  622. [section:hardware_concurrency Static member function `hardware_concurrency()`]
  623. unsigned hardware_concurrency() noexcept;
  624. [variablelist
  625. [[Returns:] [The number of hardware threads available on the current system (e.g. number of CPUs or cores or hyperthreading units),
  626. or 0 if this information is not available.]]
  627. [[Throws:] [Nothing]]
  628. ]
  629. [endsect]
  630. [section:physical_concurrency Static member function `physical_concurrency()`]
  631. unsigned physical_concurrency() noexcept;
  632. [variablelist
  633. [[Returns:] [The number of physical cores available on the current system. In contrast to `hardware_concurrency()` it does not return
  634. the number of virtual cores, but it counts only physical cores.]]
  635. [[Throws:] [Nothing]]
  636. ]
  637. [endsect]
  638. [section:nativehandle Member function `native_handle()`]
  639. typedef platform-specific-type native_handle_type;
  640. native_handle_type native_handle();
  641. [variablelist
  642. [[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
  643. implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present.]]
  644. [[Throws:] [Nothing.]]
  645. ]
  646. [endsect]
  647. [section:equals `operator==` DEPRECATED]
  648. bool operator==(const thread& other) const;
  649. [warning
  650. DEPRECATED since 4.0.0.
  651. Use `a.__get_id()==b.__get_id()` instead`.
  652. ]
  653. [variablelist
  654. [[Returns:] [`get_id()==other.get_id()`]]
  655. ]
  656. [endsect]
  657. [section:not_equals `operator!=` DEPRECATED]
  658. bool operator!=(const thread& other) const;
  659. [warning
  660. DEPRECATED since 4.0.0.
  661. Use `a.__get_id()!=b.__get_id()` instead`.
  662. ]
  663. [variablelist
  664. [[Returns:] [`get_id()!=other.get_id()`]]
  665. ]
  666. [endsect]
  667. [section:sleep Static member function `sleep()` DEPRECATED]
  668. void sleep(system_time const& abs_time);
  669. [warning
  670. DEPRECATED since 3.0.0.
  671. Use `this_thread::__sleep_for()` or `this_thread::__sleep_until()`.
  672. ]
  673. [variablelist
  674. [[Effects:] [Suspends the current thread until the specified time has been reached.]]
  675. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted.]]
  676. [[Notes:] [`sleep()` is one of the predefined __interruption_points__.]]
  677. ]
  678. [endsect]
  679. [section:yield Static member function `yield()` DEPRECATED]
  680. void yield();
  681. [warning
  682. DEPRECATED since 3.0.0.
  683. Use `this_thread::__yield()`.
  684. ]
  685. [variablelist
  686. [[Effects:] [See [link thread.thread_management.this_thread.yield `boost::this_thread::yield()`].]]
  687. ]
  688. [endsect]
  689. [section:swap Member function `swap()`]
  690. void swap(thread& other) noexcept;
  691. [variablelist
  692. [[Effects:] [Exchanges the threads of execution associated with `*this` and `other`, so `*this` is associated with the thread of
  693. execution associated with `other` prior to the call, and vice-versa.]]
  694. [[Postconditions:] [`this->get_id()` returns the same value as `other.get_id()` prior to the call. `other.get_id()` returns the same
  695. value as `this->get_id()` prior to the call.]]
  696. [[Throws:] [Nothing.]]
  697. ]
  698. [endsect]
  699. [section:non_member_swap Non-member function `swap()`]
  700. #include <boost/thread/thread.hpp>
  701. void swap(thread& lhs,thread& rhs) noexcept;
  702. [variablelist
  703. [[Effects:] [[link thread.thread_management.thread.swap `lhs.swap(rhs)`].]]
  704. ]
  705. [endsect]
  706. [section:id Class `boost::thread::id`]
  707. #include <boost/thread/thread.hpp>
  708. class thread::id
  709. {
  710. public:
  711. id() noexcept;
  712. bool operator==(const id& y) const noexcept;
  713. bool operator!=(const id& y) const noexcept;
  714. bool operator<(const id& y) const noexcept;
  715. bool operator>(const id& y) const noexcept;
  716. bool operator<=(const id& y) const noexcept;
  717. bool operator>=(const id& y) const noexcept;
  718. template<class charT, class traits>
  719. friend std::basic_ostream<charT, traits>&
  720. operator<<(std::basic_ostream<charT, traits>& os, const id& x);
  721. };
  722. [section:constructor Default constructor]
  723. id() noexcept;
  724. [variablelist
  725. [[Effects:] [Constructs a __thread_id__ instance that represents __not_a_thread__.]]
  726. [[Throws:] [Nothing]]
  727. ]
  728. [endsect]
  729. [section:is_equal `operator==`]
  730. bool operator==(const id& y) const noexcept;
  731. [variablelist
  732. [[Returns:] [`true` if `*this` and `y` both represent the same thread of execution, or both represent __not_a_thread__, `false`
  733. otherwise.]]
  734. [[Throws:] [Nothing]]
  735. ]
  736. [endsect]
  737. [section:not_equal `operator!=`]
  738. bool operator!=(const id& y) const noexcept;
  739. [variablelist
  740. [[Returns:] [`true` if `*this` and `y` represent different threads of execution, or one represents a thread of execution, and
  741. the other represent __not_a_thread__, `false` otherwise.]]
  742. [[Throws:] [Nothing]]
  743. ]
  744. [endsect]
  745. [section:less_than `operator<`]
  746. bool operator<(const id& y) const noexcept;
  747. [variablelist
  748. [[Returns:] [`true` if `*this!=y` is `true` and the implementation-defined total order of __thread_id__ values places `*this` before
  749. `y`, `false` otherwise.]]
  750. [[Throws:] [Nothing]]
  751. [[Note:] [A __thread_id__ instance representing __not_a_thread__ will always compare less than an instance representing a thread of
  752. execution.]]
  753. ]
  754. [endsect]
  755. [section:greater_than `operator>`]
  756. bool operator>(const id& y) const noexcept;
  757. [variablelist
  758. [[Returns:] [`y<*this`]]
  759. [[Throws:] [Nothing]]
  760. ]
  761. [endsect]
  762. [section:less_than_or_equal `operator<=`]
  763. bool operator<=(const id& y) const noexcept;
  764. [variablelist
  765. [[Returns:] [`!(y<*this)`]]
  766. [[Throws:] [Nothing]]
  767. ]
  768. [endsect]
  769. [section:greater_than_or_equal `operator>=`]
  770. bool operator>=(const id& y) const noexcept;
  771. [variablelist
  772. [[Returns:] [`!(*this<y)`]]
  773. [[Throws:] [Nothing]]
  774. ]
  775. [endsect]
  776. [section:stream_out Friend `operator<<`]
  777. template<class charT, class traits>
  778. friend std::basic_ostream<charT, traits>&
  779. operator<<(std::basic_ostream<charT, traits>& os, const id& x);
  780. [variablelist
  781. [[Effects:] [Writes a representation of the __thread_id__ instance `x` to the stream `os`, such that the representation of two
  782. instances of __thread_id__ `a` and `b` is the same if `a==b`, and different if `a!=b`.]]
  783. [[Returns:] [`os`]]
  784. ]
  785. [endsect]
  786. [endsect]
  787. [section:attributes Class `boost::thread::attributes` EXTENSION]
  788. class thread::attributes {
  789. public:
  790. attributes() noexcept;
  791. ~ attributes()=default;
  792. // stack
  793. void set_stack_size(std::size_t size) noexcept;
  794. std::size_t get_stack_size() const noexcept;
  795. #if defined BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE
  796. typedef platform-specific-type native_handle_type;
  797. native_handle_type* native_handle() noexcept;
  798. const native_handle_type* native_handle() const noexcept;
  799. #endif
  800. };
  801. [section:constructor Default constructor]
  802. thread_attributes() noexcept;
  803. [variablelist
  804. [[Effects:] [Constructs a thread attributes instance with its default values.]]
  805. [[Throws:] [Nothing]]
  806. ]
  807. [endsect]
  808. [section: set_stack_size Member function `set_stack_size()`]
  809. void set_stack_size(std::size_t size) noexcept;
  810. [variablelist
  811. [[Effects:] [Stores the stack size to be used to create a thread. This is a hint that the implementation can choose a better size if to small or too big or not aligned to a page.]]
  812. [[Postconditions:] [`this-> get_stack_size()` returns the chosen stack size.]]
  813. [[Throws:] [Nothing.]]
  814. ]
  815. [endsect]
  816. [section:get_stack_size Member function `get_stack_size()`]
  817. std::size_t get_stack_size() const noexcept;
  818. [variablelist
  819. [[Returns:] [The stack size to be used on the creation of a thread. Note that this function can return 0 meaning the default.]]
  820. [[Throws:] [Nothing.]]
  821. ]
  822. [endsect]
  823. [section:nativehandle Member function `native_handle()`]
  824. typedef platform-specific-type native_handle_type;
  825. typedef platform-specific-type native_handle_type;
  826. native_handle_type* native_handle() noexcept;
  827. const native_handle_type* native_handle() const noexcept;
  828. [variablelist
  829. [[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
  830. thread attributes implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present and `BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE` is not defined.]]
  831. [[Throws:] [Nothing.]]
  832. ]
  833. [endsect]
  834. [endsect] [/ thread::attributes ]
  835. [endsect] [/ thread ]
  836. [section:this_thread Namespace `this_thread`]
  837. namespace boost {
  838. namespace this_thread {
  839. thread::id get_id() noexcept;
  840. template<typename TimeDuration>
  841. void yield() noexcept;
  842. template <class Clock, class Duration>
  843. void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
  844. template <class Rep, class Period>
  845. void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  846. template<typename Callable>
  847. void at_thread_exit(Callable func); // EXTENSION
  848. void interruption_point(); // EXTENSION
  849. bool interruption_requested() noexcept; // EXTENSION
  850. bool interruption_enabled() noexcept; // EXTENSION
  851. class disable_interruption; // EXTENSION
  852. class restore_interruption; // EXTENSION
  853. #if defined BOOST_THREAD_USES_DATETIME
  854. void sleep(TimeDuration const& rel_time); // DEPRECATED
  855. void sleep(system_time const& abs_time); // DEPRECATED
  856. #endif
  857. }
  858. }
  859. [section:get_id Non-member function `get_id()`]
  860. #include <boost/thread/thread.hpp>
  861. namespace this_thread
  862. {
  863. thread::id get_id() noexcept;
  864. }
  865. [variablelist
  866. [[Returns:] [An instance of __thread_id__ that represents that currently executing thread.]]
  867. [[Throws:] [__thread_resource_error__ if an error occurs.]]
  868. ]
  869. [endsect]
  870. [section:interruption_point Non-member function `interruption_point()` EXTENSION]
  871. #include <boost/thread/thread.hpp>
  872. namespace this_thread
  873. {
  874. void interruption_point();
  875. }
  876. [variablelist
  877. [[Effects:] [Check to see if the current thread has been interrupted.]]
  878. [[Throws:] [__thread_interrupted__ if __interruption_enabled__ and __interruption_requested__ both return `true`.]]
  879. ]
  880. [endsect]
  881. [section:interruption_requested Non-member function `interruption_requested()` EXTENSION]
  882. #include <boost/thread/thread.hpp>
  883. namespace this_thread
  884. {
  885. bool interruption_requested() noexcept;
  886. }
  887. [variablelist
  888. [[Returns:] [`true` if interruption has been requested for the current thread, `false` otherwise.]]
  889. [[Throws:] [Nothing.]]
  890. ]
  891. [endsect]
  892. [section:interruption_enabled Non-member function `interruption_enabled()` EXTENSION]
  893. #include <boost/thread/thread.hpp>
  894. namespace this_thread
  895. {
  896. bool interruption_enabled() noexcept;
  897. }
  898. [variablelist
  899. [[Returns:] [`true` if interruption has been enabled for the current thread, `false` otherwise.]]
  900. [[Throws:] [Nothing.]]
  901. ]
  902. [endsect]
  903. [section:sleep Non-member function `sleep()` DEPRECATED]
  904. #include <boost/thread/thread.hpp>
  905. namespace this_thread
  906. {
  907. template<typename TimeDuration>
  908. void sleep(TimeDuration const& rel_time);
  909. void sleep(system_time const& abs_time)
  910. }
  911. [warning
  912. DEPRECATED since 3.0.0.
  913. Use `__sleep_for()` and `__sleep_until()` instead.
  914. ]
  915. [variablelist
  916. [[Effects:] [Suspends the current thread until the time period
  917. specified by `rel_time` has elapsed or the time point specified by
  918. `abs_time` has been reached.]]
  919. [[Throws:] [__thread_interrupted__ if the current thread of execution is interrupted.]]
  920. [[Notes:] [`sleep()` is one of the predefined __interruption_points__.]]
  921. ]
  922. [endsect]
  923. [section:sleep_until Non-member function `sleep_until()`]
  924. #include <boost/thread/thread.hpp>
  925. namespace this_thread
  926. {
  927. template <class Clock, class Duration>
  928. void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
  929. namespace no_interruption_point
  930. {
  931. template <class Clock, class Duration>
  932. void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
  933. }
  934. }
  935. [variablelist
  936. [[Effects:] [Suspends the current thread until the time point specified by
  937. `abs_time` has been reached.]]
  938. [[Throws:] [Nothing if Clock satisfies the TrivialClock requirements and operations of Duration
  939. do not throw exceptions. __thread_interrupted__ if the current thread of execution is interrupted. ]]
  940. [[Notes:] [`sleep_until()` is one of the predefined __interruption_points__.]]
  941. [[Notes:] [`no_interruption_point::sleep_until()` is NOT one of the __interruption_points__.]]
  942. ]
  943. [endsect]
  944. [section:sleep_for Non-member function `sleep_for()`]
  945. #include <boost/thread/thread.hpp>
  946. namespace this_thread
  947. {
  948. template <class Rep, class Period>
  949. void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  950. namespace no_interruption_point
  951. {
  952. template <class Rep, class Period>
  953. void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  954. }
  955. }
  956. [variablelist
  957. [[Effects:] [Suspends the current thread until the duration specified
  958. by `rel_time` has elapsed.]]
  959. [[Throws:] [Nothing if operations of chrono::duration<Rep, Period> do not throw exceptions. __thread_interrupted__ if the current thread of execution is interrupted.]]
  960. [[Notes:] [`sleep_for()` is one of the predefined __interruption_points__.]]
  961. [[Notes:] [`no_interruption_point:: sleep_for()` is NOT one of the __interruption_points__.]]
  962. ]
  963. [endsect]
  964. [section:yield Non-member function `yield()`]
  965. #include <boost/thread/thread.hpp>
  966. namespace this_thread
  967. {
  968. void yield() noexcept;
  969. }
  970. [variablelist
  971. [[Effects:] [Gives up the remainder of the current thread's time slice, to allow other threads to run.]]
  972. [[Throws:] [Nothing.]]
  973. ]
  974. [endsect]
  975. [section:disable_interruption Class `disable_interruption` EXTENSION]
  976. #include <boost/thread/thread.hpp>
  977. namespace this_thread
  978. {
  979. class disable_interruption
  980. {
  981. public:
  982. disable_interruption(const disable_interruption&) = delete;
  983. disable_interruption& operator=(const disable_interruption&) = delete;
  984. disable_interruption() noexcept;
  985. ~disable_interruption() noexcept;
  986. };
  987. }
  988. `boost::this_thread::disable_interruption` disables interruption for the current thread on construction, and restores the prior
  989. interruption state on destruction. Instances of `disable_interruption` cannot be copied or moved.
  990. [section:constructor Constructor]
  991. disable_interruption() noexcept;
  992. [variablelist
  993. [[Effects:] [Stores the current state of __interruption_enabled__ and disables interruption for the current thread.]]
  994. [[Postconditions:] [__interruption_enabled__ returns `false` for the current thread.]]
  995. [[Throws:] [Nothing.]]
  996. ]
  997. [endsect]
  998. [section:destructor Destructor]
  999. ~disable_interruption() noexcept;
  1000. [variablelist
  1001. [[Preconditions:] [Must be called from the same thread from which `*this` was constructed.]]
  1002. [[Effects:] [Restores the current state of __interruption_enabled__ for the current thread to that prior to the construction of `*this`.]]
  1003. [[Postconditions:] [__interruption_enabled__ for the current thread returns the value stored in the constructor of `*this`.]]
  1004. [[Throws:] [Nothing.]]
  1005. ]
  1006. [endsect]
  1007. [endsect]
  1008. [section:restore_interruption Class `restore_interruption` EXTENSION]
  1009. #include <boost/thread/thread.hpp>
  1010. namespace this_thread
  1011. {
  1012. class restore_interruption
  1013. {
  1014. public:
  1015. restore_interruption(const restore_interruption&) = delete;
  1016. restore_interruption& operator=(const restore_interruption&) = delete;
  1017. explicit restore_interruption(disable_interruption& disabler) noexcept;
  1018. ~restore_interruption() noexcept;
  1019. };
  1020. }
  1021. On construction of an instance of `boost::this_thread::restore_interruption`, the interruption state for the current thread is
  1022. restored to the interruption state stored by the constructor of the supplied instance of __disable_interruption__. When the instance
  1023. is destroyed, interruption is again disabled. Instances of `restore_interruption` cannot be copied or moved.
  1024. [section:constructor Constructor]
  1025. explicit restore_interruption(disable_interruption& disabler) noexcept;
  1026. [variablelist
  1027. [[Preconditions:] [Must be called from the same thread from which `disabler` was constructed.]]
  1028. [[Effects:] [Restores the current state of __interruption_enabled__ for the current thread to that prior to the construction of `disabler`.]]
  1029. [[Postconditions:] [__interruption_enabled__ for the current thread returns the value stored in the constructor of `disabler`.]]
  1030. [[Throws:] [Nothing.]]
  1031. ]
  1032. [endsect]
  1033. [section:destructor Destructor]
  1034. ~restore_interruption() noexcept;
  1035. [variablelist
  1036. [[Preconditions:] [Must be called from the same thread from which `*this` was constructed.]]
  1037. [[Effects:] [Disables interruption for the current thread.]]
  1038. [[Postconditions:] [__interruption_enabled__ for the current thread returns `false`.]]
  1039. [[Throws:] [Nothing.]]
  1040. ]
  1041. [endsect]
  1042. [endsect]
  1043. [section:atthreadexit Non-member function template `at_thread_exit()` EXTENSION]
  1044. #include <boost/thread/thread.hpp>
  1045. template<typename Callable>
  1046. void at_thread_exit(Callable func);
  1047. [variablelist
  1048. [[Effects:] [A copy of `func` is placed in
  1049. thread-specific storage. This copy is invoked when the current thread
  1050. exits (even if the thread has been interrupted).]]
  1051. [[Postconditions:] [A copy of `func` has been saved for invocation on thread exit.]]
  1052. [[Throws:] [`std::bad_alloc` if memory cannot be allocated for the copy of the function, __thread_resource_error__ if any other
  1053. error occurs within the thread library. Any exception thrown whilst copying `func` into internal storage.]]
  1054. [[Note:] [This function is *not* called if the thread was terminated
  1055. forcefully using platform-specific APIs, or if the thread is
  1056. terminated due to a call to `exit()`, `abort()` or
  1057. `std::terminate()`. In particular, returning from `main()` is
  1058. equivalent to call to `exit()`, so will not call any functions
  1059. registered with `at_thread_exit()`]]
  1060. ]
  1061. [endsect]
  1062. [endsect]
  1063. [section:threadgroup Class `thread_group` EXTENSION]
  1064. #include <boost/thread/thread.hpp>
  1065. class thread_group
  1066. {
  1067. public:
  1068. thread_group(const thread_group&) = delete;
  1069. thread_group& operator=(const thread_group&) = delete;
  1070. thread_group();
  1071. ~thread_group();
  1072. template<typename F>
  1073. thread* create_thread(F threadfunc);
  1074. void add_thread(thread* thrd);
  1075. void remove_thread(thread* thrd);
  1076. bool is_this_thread_in();
  1077. bool is_thread_in(thread* thrd);
  1078. void join_all();
  1079. void interrupt_all();
  1080. int size() const;
  1081. };
  1082. `thread_group` provides for a collection of threads that are related in some fashion. New threads can be added to the group with
  1083. `add_thread` and `create_thread` member functions. `thread_group` is not copyable or movable.
  1084. [section:constructor Constructor]
  1085. thread_group();
  1086. [variablelist
  1087. [[Effects:] [Create a new thread group with no threads.]]
  1088. ]
  1089. [endsect]
  1090. [section:destructor Destructor]
  1091. ~thread_group();
  1092. [variablelist
  1093. [[Effects:] [Destroy `*this` and `delete` all __thread__ objects in the group.]]
  1094. ]
  1095. [endsect]
  1096. [section:create_thread Member function `create_thread()`]
  1097. template<typename F>
  1098. thread* create_thread(F threadfunc);
  1099. [variablelist
  1100. [[Effects:] [Create a new __thread__ object as-if by `new thread(threadfunc)` and add it to the group.]]
  1101. [[Postcondition:] [`this->size()` is increased by one, the new thread is running.]]
  1102. [[Returns:] [A pointer to the new __thread__ object.]]
  1103. ]
  1104. [endsect]
  1105. [section:add_thread Member function `add_thread()`]
  1106. void add_thread(thread* thrd);
  1107. [variablelist
  1108. [[Precondition:] [The expression `delete thrd` is well-formed and will not result in undefined behaviour and `is_thread_in(thrd) == false`.]]
  1109. [[Effects:] [Take ownership of the __thread__ object pointed to by `thrd` and add it to the group.]]
  1110. [[Postcondition:] [`this->size()` is increased by one.]]
  1111. ]
  1112. [endsect]
  1113. [section:remove_thread Member function `remove_thread()`]
  1114. void remove_thread(thread* thrd);
  1115. [variablelist
  1116. [[Effects:] [If `thrd` is a member of the group, remove it without calling `delete`.]]
  1117. [[Postcondition:] [If `thrd` was a member of the group, `this->size()` is decreased by one.]]
  1118. ]
  1119. [endsect]
  1120. [section:join_all Member function `join_all()`]
  1121. void join_all();
  1122. [variablelist
  1123. [[Requires:] [`is_this_thread_in() == false`.]]
  1124. [[Effects:] [Call `join()` on each __thread__ object in the group.]]
  1125. [[Postcondition:] [Every thread in the group has terminated.]]
  1126. [[Note:] [Since __join__ is one of the predefined __interruption_points__, `join_all()` is also an interruption point.]]
  1127. ]
  1128. [endsect]
  1129. [section:is_this_thread_in Member function `is_this_thread_in()`]
  1130. bool is_this_thread_in();
  1131. [variablelist
  1132. [[Returns:] [true if there is a thread `th` in the group such that `th.get_id() == this_thread::get_id()`.]]
  1133. ]
  1134. [endsect]
  1135. [section:is_thread_in Member function `is_thread_in()`]
  1136. bool is_thread_in(thread* thrd);
  1137. [variablelist
  1138. [[Returns:] [true if there is a thread `th` in the group such that `th.get_id() == thrd->get_id()`.]]
  1139. ]
  1140. [endsect]
  1141. [section:interrupt_all Member function `interrupt_all()`]
  1142. void interrupt_all();
  1143. [variablelist
  1144. [[Effects:] [Call `interrupt()` on each __thread__ object in the group.]]
  1145. ]
  1146. [endsect]
  1147. [section:size Member function `size()`]
  1148. int size();
  1149. [variablelist
  1150. [[Returns:] [The number of threads in the group.]]
  1151. [[Throws:] [Nothing.]]
  1152. ]
  1153. [endsect]
  1154. [endsect]
  1155. [endsect]