2 (C) Copyright 20012 Vicente J. Botet Escriba.
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
9 [section:emulations Emulations]
10 [section:delete `=delete` emulation]
12 C++11 allows to delete some implicitly generated functions as constructors and assignment using '= delete' as in
15 thread(thread const&) = delete;
17 On compilers not supporting this feature, Boost.Thread relays on a partial simulation, it declares the function as private without definition.
22 The emulation is partial as the private function can be used for overload resolution for some compilers and prefer it to other overloads that need a conversion. See below the consequences on the move semantic emulation.
26 [section:move Move semantics]
28 In order to implement Movable classes, move parameters and return types Boost.Thread uses the rvalue reference when the compiler support it.
29 On compilers not supporting it Boost.Thread uses either the emulation provided by Boost.Move or the emulation provided by the previous versions of Boost.Thread depending whether `BOOST_THREAD_USES_MOVE` is defined or not. This macros is unset by default when `BOOST_THREAD_VERSION` is 2. Since `BOOST_THREAD_VERSION` 3, `BOOST_THREAD_USES_MOVE` is defined.
31 [section:deprecated Deprecated Version 2 interface]
33 Previous to version 1.50, Boost.Thread make use of its own move semantic emulation which had more limitations than the provided by Boost.Move. In addition, it is of interest of the whole Boost community that Boost.Thread uses Boost.Move so that boost::thread can be stored on Movable aware containers.
35 To preserve backward compatibility at least during some releases, Boost.Thread allows the user to use the deprecated move semantic emulation defining BOOST_THREAD_DONT_USE_MOVE.
37 Many aspects of move semantics can be emulated for compilers not supporting rvalue references and Boost.Thread legacy offers tools for that purpose.
39 [section:Helper Helpers class and function]
41 Next follows the interface of the legacy move semantic helper class and function.
50 explicit thread_move_t(T& t_);
52 T* operator->() const;
54 void operator=(thread_move_t&);
58 boost::detail::thread_move_t<T> move(boost::detail::thread_move_t<T> t);
62 [section:movable Movable emulation]
64 We can write a MovableOny class as follows. You just need to follow these simple steps:
66 * Add a conversion to the `detail::thread_move_t<classname>`
67 * Make the copy constructor private.
68 * Write a constructor taking the parameter as `detail::thread_move_t<classname>`
69 * Write an assignment taking the parameter as `detail::thread_move_t<classname>`
71 For example the thread class defines the following:
78 thread& operator=(thread&);
80 detail::thread_move_t<thread> move()
82 detail::thread_move_t<thread> x(*this);
85 operator detail::thread_move_t<thread>()
89 thread(detail::thread_move_t<thread> x)
91 thread_info=x->thread_info;
92 x->thread_info.reset();
94 thread& operator=(detail::thread_move_t<thread> x)
108 [section:portable Portable interface]
110 In order to make the library code portable Boost.Thread uses some macros that will use either the ones provided by Boost.Move or the deprecated move semantics provided by previous versions of Boost.Thread.
112 See the Boost.Move documentation for a complete description on how to declare new Movable classes and its limitations.
114 * `BOOST_THREAD_RV_REF(TYPE)` is the equivalent of `BOOST_RV_REF(TYPE)`
115 * `BOOST_THREAD_RV_REF_BEG` is the equivalent of `BOOST_RV_REF_BEG(TYPE)`
116 * `BOOST_THREAD_RV_REF_END` is the equivalent of `BOOST_RV_REF_END(TYPE)`
117 * `BOOST_THREAD_FWD_REF(TYPE)` is the equivalent of `BOOST_FWD_REF(TYPE)
119 In addition the following macros are needed to make the code portable:
121 * `BOOST_THREAD_RV(V)` macro to access the rvalue from a BOOST_THREAD_RV_REF(TYPE),
122 * `BOOST_THREAD_MAKE_RV_REF(RVALUE)` makes a rvalue.
123 * `BOOST_THREAD_DCL_MOVABLE(CLASS)` to avoid conflicts with Boost.Move
124 * `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END` are variant of `BOOST_THREAD_DCL_MOVABLE` when the parameter is a template instantiation.
126 Other macros are provided and must be included on the public section:
128 * `BOOST_THREAD_NO_COPYABLE` declares a class no-copyable either deleting the copy constructors and copy assignment or moving them to the private section.
129 * `BOOST_THREAD_MOVABLE(CLASS)` declares all the implicit conversions to an rvalue-reference.
130 * `BOOST_THREAD_MOVABLE_ONLY(CLASS)` is the equivalent of `BOOST_MOVABLE_BUT_NOT_COPYABLE(CLASS)`
131 * `BOOST_THREAD_COPYABLE_AND_MOVABLE(CLASS)` is the equivalent of `BOOST_COPYABLE_AND_MOVABLE(CLASS)`
134 [section:NO_COPYABLE `BOOST_THREAD_NO_COPYABLE(CLASS)`]
136 This macro marks a class as no copyable, disabling copy construction and assignment.
140 [section:MOVABLE `BOOST_THREAD_MOVABLE(CLASS)`]
142 This macro marks a class as movable, declaring all the implicit conversions to an rvalue-reference.
146 [section:MOVABLE_ONLY `BOOST_THREAD_MOVABLE_ONLY(CLASS)`]
148 This macro marks a type as movable but not copyable, disabling copy construction and assignment. The user will need to write a move constructor/assignment to fully write a movable but not copyable class.
152 [section:COPYABLE_AND_MOVABLE `BOOST_THREAD_COPYABLE_AND_MOVABLE(CLASS)`]
154 This macro marks a type as copyable and movable. The user will need to write a move constructor/assignment and a copy assignment to fully write a copyable and movable class.
158 [section:RV_REF `BOOST_THREAD_RV_REF(TYPE)`, `BOOST_THREAD_RV_REF_BEG` and `BOOST_THREAD_RV_REF_END`]
160 This macro is used to achieve portable syntax in move constructors and assignments for classes marked as `BOOST_THREAD_COPYABLE_AND_MOVABLE` or `BOOST_THREAD_MOVABLE_ONLY`.
162 `BOOST_THREAD_RV_REF_BEG` and `BOOST_THREAD_RV_REF_END` are used when the parameter end with a `>` to avoid the compiler error.
166 [section:RV `BOOST_THREAD_RV(V)`]
168 While Boost.Move emulation allows to access an rvalue reference `BOOST_THREAD_RV_REF(TYPE)` using the dot operator, the legacy defines the `operator->`. We need then a macro `BOOST_THREAD_RV` that mask this difference. E.g.
170 thread(BOOST_THREAD_RV_REF(thread) x)
172 thread_info=BOOST_THREAD_RV(x).thread_info;
173 BOOST_THREAD_RV(x).thread_info.reset();
176 The use of this macros has reduced considerably the size of the Boost.Thread move related code.
180 [section:MAKE_RV_REF `BOOST_THREAD_MAKE_RV_REF(RVALUE)`]
182 While Boost.Move is the best C++03 move emulation there are some limitations that impact the way the library can be used.
183 For example, with the following declarations
194 This could not work on some compilers even if thread is convertible to `rv<thread>` because the compiler prefers the private copy constructor.
201 On these compilers we need to use instead an explicit conversion. The library provides a move member function that allows to workaround the issue.
205 return thread(f).move();
208 Note that `::boost::move` can not be used in this case as thread is not implicitly convertible to `thread&`.
212 return ::boost::move(thread(f));
215 To make the code portable Boost.Thread the user needs to use a macro `BOOST_THREAD_MAKE_RV_REF` that can be used as in
219 return BOOST_THREAD_MAKE_RV_REF(thread(f));
222 Note that this limitation is shared also by the legacy Boost.Thread move emulation.
226 [section:DCL_MOVABLE `BOOST_THREAD_DCL_MOVABLE`, `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END`]
228 As Boost.Move defines also the `boost::move` function we need to specialize the `has_move_emulation_enabled_aux` metafunction.
231 struct has_move_emulation_enabled_aux<thread>
232 : BOOST_MOVE_BOOST_NS::integral_constant<bool, true>
235 so that the following Boost.Move overload is disabled
238 inline typename BOOST_MOVE_BOOST_NS::disable_if<has_move_emulation_enabled_aux<T>, T&>::type move(T& x);
240 The macros `BOOST_THREAD_DCL_MOVABLE(CLASS)`, `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END` are used for this purpose. E.g.
242 BOOST_THREAD_DCL_MOVABLE(thread)
246 BOOST_THREAD_DCL_MOVABLE_BEG(T) promise<T> BOOST_THREAD_DCL_MOVABLE_END
255 [section:bool_explicit_conversion Bool explicit conversion]
257 Locks provide an explicit bool conversion operator when the compiler provides them.
259 explicit operator bool() const;
261 The library provides un implicit conversion to an undefined type that can be used as a conditional expression.
263 #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
264 operator ``['unspecified-bool-type]``() const;
265 bool operator!() const;
267 explicit operator bool() const;
270 The user should use the lock.owns_lock() when an explicit conversion is required.
272 [section:bool_conversion `operator `['unspecified-bool-type]`() const`]
276 [[Returns:] [If __owns_lock_ref__ would return `true`, a value that evaluates to
277 `true` in boolean contexts, otherwise a value that evaluates to `false` in
280 [[Throws:] [Nothing.]]
287 [section:operator_not `bool operator!() const`]
291 [[Returns:] [`!` __owns_lock_ref__.]]
293 [[Throws:] [Nothing.]]
302 [section:scoped_enums Scoped Enums]
304 Some of the enumerations defined in the standard library are scoped enums.
306 On compilers that don't support them, the library uses a class to wrap the underlying type. Instead of
308 enum class future_errc
311 future_already_retrieved,
312 promise_already_satisfied,
316 the library declare these types as
318 BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
321 future_already_retrieved,
322 promise_already_satisfied,
325 BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
327 These macros allows to use 'future_errc' in almost all the cases as a scoped enum.
329 There are however some limitations:
331 * The type is not a C++ enum, so 'is_enum<future_errc>' will be false_type.
332 * The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some macros.
338 case future_errc::broken_promise:
343 switch (boost::native_value(ev))
345 case future_errc::broken_promise:
349 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
351 struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc> : public true_type { };
356 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
358 struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc::enum_type> : public true_type { };