• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Emulations</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../thread.html" title="Chapter 39. Thread 4.8.0">
10<link rel="prev" href="time.html" title="Time Requirements">
11<link rel="next" href="acknowledgements.html" title="Acknowledgments">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="time.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../thread.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="thread.emulations"></a><a class="link" href="emulations.html" title="Emulations">Emulations</a>
29</h2></div></div></div>
30<div class="toc"><dl class="toc">
31<dt><span class="section"><a href="emulations.html#thread.emulations.delete"><code class="computeroutput"><span class="special">=</span><span class="keyword">delete</span></code> emulation</a></span></dt>
32<dt><span class="section"><a href="emulations.html#thread.emulations.move">Move semantics</a></span></dt>
33<dt><span class="section"><a href="emulations.html#thread.emulations.bool_explicit_conversion">Bool explicit
34      conversion</a></span></dt>
35<dt><span class="section"><a href="emulations.html#thread.emulations.scoped_enums">Scoped Enums</a></span></dt>
36</dl></div>
37<div class="section">
38<div class="titlepage"><div><div><h3 class="title">
39<a name="thread.emulations.delete"></a><a class="link" href="emulations.html#thread.emulations.delete" title="=delete emulation"><code class="computeroutput"><span class="special">=</span><span class="keyword">delete</span></code> emulation</a>
40</h3></div></div></div>
41<p>
42        C++11 allows to delete some implicitly generated functions as constructors
43        and assignment using '= delete' as in
44      </p>
45<pre class="programlisting"><span class="keyword">public</span><span class="special">:</span>
46  <span class="identifier">thread</span><span class="special">(</span><span class="identifier">thread</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span>
47</pre>
48<p>
49        On compilers not supporting this feature, Boost.Thread relays on a partial
50        simulation, it declares the function as private without definition.
51      </p>
52<pre class="programlisting"><span class="keyword">private</span><span class="special">:</span>
53  <span class="identifier">thread</span><span class="special">(</span><span class="identifier">thread</span> <span class="special">&amp;);</span>
54</pre>
55<p>
56        The emulation is partial as the private function can be used for overload
57        resolution for some compilers and prefer it to other overloads that need
58        a conversion. See below the consequences on the move semantic emulation.
59      </p>
60</div>
61<div class="section">
62<div class="titlepage"><div><div><h3 class="title">
63<a name="thread.emulations.move"></a><a class="link" href="emulations.html#thread.emulations.move" title="Move semantics">Move semantics</a>
64</h3></div></div></div>
65<div class="toc"><dl class="toc">
66<dt><span class="section"><a href="emulations.html#thread.emulations.move.deprecated">Deprecated Version
67        2 interface</a></span></dt>
68<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable">Portable interface</a></span></dt>
69</dl></div>
70<p>
71        In order to implement Movable classes, move parameters and return types Boost.Thread
72        uses the rvalue reference when the compiler support it. On compilers not
73        supporting it Boost.Thread uses either the emulation provided by Boost.Move
74        or the emulation provided by the previous versions of Boost.Thread depending
75        whether <code class="computeroutput"><span class="identifier">BOOST_THREAD_USES_MOVE</span></code>
76        is defined or not. This macros is unset by default when <code class="computeroutput"><span class="identifier">BOOST_THREAD_VERSION</span></code>
77        is 2. Since <code class="computeroutput"><span class="identifier">BOOST_THREAD_VERSION</span></code>
78        3, <code class="computeroutput"><span class="identifier">BOOST_THREAD_USES_MOVE</span></code>
79        is defined.
80      </p>
81<div class="section">
82<div class="titlepage"><div><div><h4 class="title">
83<a name="thread.emulations.move.deprecated"></a><a class="link" href="emulations.html#thread.emulations.move.deprecated" title="Deprecated Version 2 interface">Deprecated Version
84        2 interface</a>
85</h4></div></div></div>
86<div class="toc"><dl class="toc">
87<dt><span class="section"><a href="emulations.html#thread.emulations.move.deprecated.Helper">Helpers
88          class and function</a></span></dt>
89<dt><span class="section"><a href="emulations.html#thread.emulations.move.deprecated.movable">Movable
90          emulation</a></span></dt>
91</dl></div>
92<p>
93          Previous to version 1.50, Boost.Thread make use of its own move semantic
94          emulation which had more limitations than the provided by Boost.Move. In
95          addition, it is of interest of the whole Boost community that Boost.Thread
96          uses Boost.Move so that boost::thread can be stored on Movable aware containers.
97        </p>
98<p>
99          To preserve backward compatibility at least during some releases, Boost.Thread
100          allows the user to use the deprecated move semantic emulation defining
101          BOOST_THREAD_DONT_USE_MOVE.
102        </p>
103<p>
104          Many aspects of move semantics can be emulated for compilers not supporting
105          rvalue references and Boost.Thread legacy offers tools for that purpose.
106        </p>
107<div class="section">
108<div class="titlepage"><div><div><h5 class="title">
109<a name="thread.emulations.move.deprecated.Helper"></a><a class="link" href="emulations.html#thread.emulations.move.deprecated.Helper" title="Helpers class and function">Helpers
110          class and function</a>
111</h5></div></div></div>
112<p>
113            Next follows the interface of the legacy move semantic helper class and
114            function.
115          </p>
116<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
117<span class="special">{</span>
118  <span class="keyword">namespace</span> <span class="identifier">detail</span>
119  <span class="special">{</span>
120      <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
121      <span class="keyword">struct</span> <span class="identifier">thread_move_t</span>
122      <span class="special">{</span>
123        <span class="keyword">explicit</span> <span class="identifier">thread_move_t</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">t_</span><span class="special">);</span>
124        <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span><span class="special">;</span>
125        <span class="identifier">T</span><span class="special">*</span> <span class="keyword">operator</span><span class="special">-&gt;()</span> <span class="keyword">const</span><span class="special">;</span>
126      <span class="keyword">private</span><span class="special">:</span>
127        <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">thread_move_t</span><span class="special">&amp;);</span>
128      <span class="special">};</span>
129  <span class="special">}</span>
130  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
131  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">move</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">t</span><span class="special">);</span>
132<span class="special">}</span>
133</pre>
134</div>
135<div class="section">
136<div class="titlepage"><div><div><h5 class="title">
137<a name="thread.emulations.move.deprecated.movable"></a><a class="link" href="emulations.html#thread.emulations.move.deprecated.movable" title="Movable emulation">Movable
138          emulation</a>
139</h5></div></div></div>
140<p>
141            We can write a MovableOny class as follows. You just need to follow these
142            simple steps:
143          </p>
144<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
145<li class="listitem">
146                Add a conversion to the <code class="computeroutput"><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">classname</span><span class="special">&gt;</span></code>
147              </li>
148<li class="listitem">
149                Make the copy constructor private.
150              </li>
151<li class="listitem">
152                Write a constructor taking the parameter as <code class="computeroutput"><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">classname</span><span class="special">&gt;</span></code>
153              </li>
154<li class="listitem">
155                Write an assignment taking the parameter as <code class="computeroutput"><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">classname</span><span class="special">&gt;</span></code>
156              </li>
157</ul></div>
158<p>
159            For example the thread class defines the following:
160          </p>
161<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">thread</span>
162<span class="special">{</span>
163  <span class="comment">// ...</span>
164<span class="keyword">private</span><span class="special">:</span>
165    <span class="identifier">thread</span><span class="special">(</span><span class="identifier">thread</span><span class="special">&amp;);</span>
166    <span class="identifier">thread</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">thread</span><span class="special">&amp;);</span>
167<span class="keyword">public</span><span class="special">:</span>
168    <span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span> <span class="identifier">move</span><span class="special">()</span>
169    <span class="special">{</span>
170        <span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">(*</span><span class="keyword">this</span><span class="special">);</span>
171        <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span>
172    <span class="special">}</span>
173    <span class="keyword">operator</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;()</span>
174    <span class="special">{</span>
175        <span class="keyword">return</span> <span class="identifier">move</span><span class="special">();</span>
176    <span class="special">}</span>
177    <span class="identifier">thread</span><span class="special">(</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">)</span>
178    <span class="special">{</span>
179        <span class="identifier">thread_info</span><span class="special">=</span><span class="identifier">x</span><span class="special">-&gt;</span><span class="identifier">thread_info</span><span class="special">;</span>
180        <span class="identifier">x</span><span class="special">-&gt;</span><span class="identifier">thread_info</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span>
181    <span class="special">}</span>
182    <span class="identifier">thread</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_move_t</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">)</span>
183    <span class="special">{</span>
184        <span class="identifier">thread</span> <span class="identifier">new_thread</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
185        <span class="identifier">swap</span><span class="special">(</span><span class="identifier">new_thread</span><span class="special">);</span>
186        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
187    <span class="special">}</span>
188  <span class="comment">// ...</span>
189
190<span class="special">};</span>
191</pre>
192</div>
193</div>
194<div class="section">
195<div class="titlepage"><div><div><h4 class="title">
196<a name="thread.emulations.move.portable"></a><a class="link" href="emulations.html#thread.emulations.move.portable" title="Portable interface">Portable interface</a>
197</h4></div></div></div>
198<div class="toc"><dl class="toc">
199<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.NO_COPYABLE"><code class="computeroutput"><span class="identifier">BOOST_THREAD_NO_COPYABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a></span></dt>
200<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.MOVABLE"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a></span></dt>
201<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.MOVABLE_ONLY"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE_ONLY</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a></span></dt>
202<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.COPYABLE_AND_MOVABLE"><code class="computeroutput"><span class="identifier">BOOST_THREAD_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a></span></dt>
203<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.RV_REF"><code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>,
204          <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_BEG</span></code>
205          and <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_END</span></code></a></span></dt>
206<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.RV"><code class="computeroutput"><span class="identifier">BOOST_THREAD_RV</span><span class="special">(</span><span class="identifier">V</span><span class="special">)</span></code></a></span></dt>
207<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.MAKE_RV_REF"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MAKE_RV_REF</span><span class="special">(</span><span class="identifier">RVALUE</span><span class="special">)</span></code></a></span></dt>
208<dt><span class="section"><a href="emulations.html#thread.emulations.move.portable.DCL_MOVABLE"><code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span></code>, <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_BEG</span><span class="special">(</span><span class="identifier">T1</span><span class="special">)</span></code> and
209          <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_END</span></code></a></span></dt>
210</dl></div>
211<p>
212          In order to make the library code portable Boost.Thread uses some macros
213          that will use either the ones provided by Boost.Move or the deprecated
214          move semantics provided by previous versions of Boost.Thread.
215        </p>
216<p>
217          See the Boost.Move documentation for a complete description on how to declare
218          new Movable classes and its limitations.
219        </p>
220<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
221<li class="listitem">
222              <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code> is the equivalent of <code class="computeroutput"><span class="identifier">BOOST_RV_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>
223            </li>
224<li class="listitem">
225              <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_BEG</span></code>
226              is the equivalent of <code class="computeroutput"><span class="identifier">BOOST_RV_REF_BEG</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>
227            </li>
228<li class="listitem">
229              <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_END</span></code>
230              is the equivalent of <code class="computeroutput"><span class="identifier">BOOST_RV_REF_END</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>
231            </li>
232<li class="listitem">
233              <code class="computeroutput"><span class="identifier">BOOST_THREAD_FWD_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code> is the equivalent of `BOOST_FWD_REF(TYPE)
234            </li>
235</ul></div>
236<p>
237          In addition the following macros are needed to make the code portable:
238        </p>
239<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
240<li class="listitem">
241              <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV</span><span class="special">(</span><span class="identifier">V</span><span class="special">)</span></code>
242              macro to access the rvalue from a BOOST_THREAD_RV_REF(TYPE),
243            </li>
244<li class="listitem">
245              <code class="computeroutput"><span class="identifier">BOOST_THREAD_MAKE_RV_REF</span><span class="special">(</span><span class="identifier">RVALUE</span><span class="special">)</span></code> makes a rvalue.
246            </li>
247<li class="listitem">
248              <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code> to avoid conflicts with Boost.Move
249            </li>
250<li class="listitem">
251              <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_BEG</span><span class="special">(</span><span class="identifier">T1</span><span class="special">)</span></code> and <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_END</span></code>
252              are variant of <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span></code>
253              when the parameter is a template instantiation.
254            </li>
255</ul></div>
256<p>
257          Other macros are provided and must be included on the public section:
258        </p>
259<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
260<li class="listitem">
261              <code class="computeroutput"><span class="identifier">BOOST_THREAD_NO_COPYABLE</span></code>
262              declares a class no-copyable either deleting the copy constructors
263              and copy assignment or moving them to the private section.
264            </li>
265<li class="listitem">
266              <code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code> declares all the implicit conversions
267              to an rvalue-reference.
268            </li>
269<li class="listitem">
270              <code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE_ONLY</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code> is the equivalent of <code class="computeroutput"><span class="identifier">BOOST_MOVABLE_BUT_NOT_COPYABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code>
271            </li>
272<li class="listitem">
273              <code class="computeroutput"><span class="identifier">BOOST_THREAD_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code> is the equivalent of <code class="computeroutput"><span class="identifier">BOOST_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code>
274            </li>
275</ul></div>
276<div class="section">
277<div class="titlepage"><div><div><h5 class="title">
278<a name="thread.emulations.move.portable.NO_COPYABLE"></a><a class="link" href="emulations.html#thread.emulations.move.portable.NO_COPYABLE" title="BOOST_THREAD_NO_COPYABLE(CLASS)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_NO_COPYABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a>
279</h5></div></div></div>
280<p>
281            This macro marks a class as no copyable, disabling copy construction
282            and assignment.
283          </p>
284</div>
285<div class="section">
286<div class="titlepage"><div><div><h5 class="title">
287<a name="thread.emulations.move.portable.MOVABLE"></a><a class="link" href="emulations.html#thread.emulations.move.portable.MOVABLE" title="BOOST_THREAD_MOVABLE(CLASS)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a>
288</h5></div></div></div>
289<p>
290            This macro marks a class as movable, declaring all the implicit conversions
291            to an rvalue-reference.
292          </p>
293</div>
294<div class="section">
295<div class="titlepage"><div><div><h5 class="title">
296<a name="thread.emulations.move.portable.MOVABLE_ONLY"></a><a class="link" href="emulations.html#thread.emulations.move.portable.MOVABLE_ONLY" title="BOOST_THREAD_MOVABLE_ONLY(CLASS)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE_ONLY</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a>
297</h5></div></div></div>
298<p>
299            This macro marks a type as movable but not copyable, disabling copy construction
300            and assignment. The user will need to write a move constructor/assignment
301            to fully write a movable but not copyable class.
302          </p>
303</div>
304<div class="section">
305<div class="titlepage"><div><div><h5 class="title">
306<a name="thread.emulations.move.portable.COPYABLE_AND_MOVABLE"></a><a class="link" href="emulations.html#thread.emulations.move.portable.COPYABLE_AND_MOVABLE" title="BOOST_THREAD_COPYABLE_AND_MOVABLE(CLASS)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_COPYABLE_AND_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code></a>
307</h5></div></div></div>
308<p>
309            This macro marks a type as copyable and movable. The user will need to
310            write a move constructor/assignment and a copy assignment to fully write
311            a copyable and movable class.
312          </p>
313</div>
314<div class="section">
315<div class="titlepage"><div><div><h5 class="title">
316<a name="thread.emulations.move.portable.RV_REF"></a><a class="link" href="emulations.html#thread.emulations.move.portable.RV_REF" title="BOOST_THREAD_RV_REF(TYPE), BOOST_THREAD_RV_REF_BEG and BOOST_THREAD_RV_REF_END"><code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>,
317          <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_BEG</span></code>
318          and <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_END</span></code></a>
319</h5></div></div></div>
320<p>
321            This macro is used to achieve portable syntax in move constructors and
322            assignments for classes marked as <code class="computeroutput"><span class="identifier">BOOST_THREAD_COPYABLE_AND_MOVABLE</span></code>
323            or <code class="computeroutput"><span class="identifier">BOOST_THREAD_MOVABLE_ONLY</span></code>.
324          </p>
325<p>
326            <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_BEG</span></code>
327            and <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF_END</span></code>
328            are used when the parameter end with a <code class="computeroutput"><span class="special">&gt;</span></code>
329            to avoid the compiler error.
330          </p>
331</div>
332<div class="section">
333<div class="titlepage"><div><div><h5 class="title">
334<a name="thread.emulations.move.portable.RV"></a><a class="link" href="emulations.html#thread.emulations.move.portable.RV" title="BOOST_THREAD_RV(V)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_RV</span><span class="special">(</span><span class="identifier">V</span><span class="special">)</span></code></a>
335</h5></div></div></div>
336<p>
337            While Boost.Move emulation allows to access an rvalue reference <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV_REF</span><span class="special">(</span><span class="identifier">TYPE</span><span class="special">)</span></code>
338            using the dot operator, the legacy defines the <code class="computeroutput"><span class="keyword">operator</span><span class="special">-&gt;</span></code>. We need then a macro <code class="computeroutput"><span class="identifier">BOOST_THREAD_RV</span></code> that mask this difference.
339            E.g.
340          </p>
341<pre class="programlisting"><span class="identifier">thread</span><span class="special">(</span><span class="identifier">BOOST_THREAD_RV_REF</span><span class="special">(</span><span class="identifier">thread</span><span class="special">)</span> <span class="identifier">x</span><span class="special">)</span>
342<span class="special">{</span>
343    <span class="identifier">thread_info</span><span class="special">=</span><span class="identifier">BOOST_THREAD_RV</span><span class="special">(</span><span class="identifier">x</span><span class="special">).</span><span class="identifier">thread_info</span><span class="special">;</span>
344    <span class="identifier">BOOST_THREAD_RV</span><span class="special">(</span><span class="identifier">x</span><span class="special">).</span><span class="identifier">thread_info</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span>
345<span class="special">}</span>
346</pre>
347<p>
348            The use of this macros has reduced considerably the size of the Boost.Thread
349            move related code.
350          </p>
351</div>
352<div class="section">
353<div class="titlepage"><div><div><h5 class="title">
354<a name="thread.emulations.move.portable.MAKE_RV_REF"></a><a class="link" href="emulations.html#thread.emulations.move.portable.MAKE_RV_REF" title="BOOST_THREAD_MAKE_RV_REF(RVALUE)"><code class="computeroutput"><span class="identifier">BOOST_THREAD_MAKE_RV_REF</span><span class="special">(</span><span class="identifier">RVALUE</span><span class="special">)</span></code></a>
355</h5></div></div></div>
356<p>
357            While Boost.Move is the best C++03 move emulation there are some limitations
358            that impact the way the library can be used. For example, with the following
359            declarations
360          </p>
361<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">thread</span> <span class="special">{</span>
362  <span class="comment">// ...</span>
363<span class="keyword">private</span><span class="special">:</span>
364  <span class="identifier">thread</span><span class="special">(</span><span class="identifier">thread</span> <span class="special">&amp;);</span>
365<span class="keyword">public</span><span class="special">:</span>
366  <span class="identifier">thread</span><span class="special">(</span><span class="identifier">rv</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;&amp;);</span>
367  <span class="comment">// ...</span>
368<span class="special">};</span>
369</pre>
370<p>
371            This could not work on some compilers even if thread is convertible to
372            <code class="computeroutput"><span class="identifier">rv</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span></code>
373            because the compiler prefers the private copy constructor.
374          </p>
375<pre class="programlisting"><span class="identifier">thread</span> <span class="identifier">mkth</span><span class="special">()</span>
376<span class="special">{</span>
377  <span class="keyword">return</span> <span class="identifier">thread</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span>
378<span class="special">}</span>
379</pre>
380<p>
381            On these compilers we need to use instead an explicit conversion. The
382            library provides a move member function that allows to workaround the
383            issue.
384          </p>
385<pre class="programlisting"><span class="identifier">thread</span> <span class="identifier">mkth</span><span class="special">()</span>
386<span class="special">{</span>
387  <span class="keyword">return</span> <span class="identifier">thread</span><span class="special">(</span><span class="identifier">f</span><span class="special">).</span><span class="identifier">move</span><span class="special">();</span>
388<span class="special">}</span>
389</pre>
390<p>
391            Note that <code class="computeroutput"><span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span></code>
392            can not be used in this case as thread is not implicitly convertible
393            to <code class="computeroutput"><span class="identifier">thread</span><span class="special">&amp;</span></code>.
394          </p>
395<pre class="programlisting"><span class="identifier">thread</span> <span class="identifier">mkth</span><span class="special">()</span>
396<span class="special">{</span>
397  <span class="keyword">return</span> <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">thread</span><span class="special">(</span><span class="identifier">f</span><span class="special">));</span>
398<span class="special">}</span>
399</pre>
400<p>
401            To make the code portable Boost.Thread the user needs to use a macro
402            <code class="computeroutput"><span class="identifier">BOOST_THREAD_MAKE_RV_REF</span></code>
403            that can be used as in
404          </p>
405<pre class="programlisting"><span class="identifier">thread</span> <span class="identifier">mkth</span><span class="special">()</span>
406<span class="special">{</span>
407  <span class="keyword">return</span> <span class="identifier">BOOST_THREAD_MAKE_RV_REF</span><span class="special">(</span><span class="identifier">thread</span><span class="special">(</span><span class="identifier">f</span><span class="special">));</span>
408<span class="special">}</span>
409</pre>
410<p>
411            Note that this limitation is shared also by the legacy Boost.Thread move
412            emulation.
413          </p>
414</div>
415<div class="section">
416<div class="titlepage"><div><div><h5 class="title">
417<a name="thread.emulations.move.portable.DCL_MOVABLE"></a><a class="link" href="emulations.html#thread.emulations.move.portable.DCL_MOVABLE" title="BOOST_THREAD_DCL_MOVABLE, BOOST_THREAD_DCL_MOVABLE_BEG(T1) and BOOST_THREAD_DCL_MOVABLE_END"><code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span></code>, <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_BEG</span><span class="special">(</span><span class="identifier">T1</span><span class="special">)</span></code> and
418          <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_END</span></code></a>
419</h5></div></div></div>
420<p>
421            As Boost.Move defines also the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span></code>
422            function we need to specialize the <code class="computeroutput"><span class="identifier">has_move_emulation_enabled_aux</span></code>
423            metafunction.
424          </p>
425<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;&gt;</span>
426<span class="keyword">struct</span> <span class="identifier">has_move_emulation_enabled_aux</span><span class="special">&lt;</span><span class="identifier">thread</span><span class="special">&gt;</span>
427  <span class="special">:</span> <span class="identifier">BOOST_MOVE_BOOST_NS</span><span class="special">::</span><span class="identifier">integral_constant</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">,</span> <span class="keyword">true</span><span class="special">&gt;</span>
428<span class="special">{};</span>
429</pre>
430<p>
431            so that the following Boost.Move overload is disabled
432          </p>
433<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
434<span class="keyword">inline</span> <span class="keyword">typename</span> <span class="identifier">BOOST_MOVE_BOOST_NS</span><span class="special">::</span><span class="identifier">disable_if</span><span class="special">&lt;</span><span class="identifier">has_move_emulation_enabled_aux</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;,</span> <span class="identifier">T</span><span class="special">&amp;&gt;::</span><span class="identifier">type</span> <span class="identifier">move</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
435</pre>
436<p>
437            The macros <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span><span class="special">(</span><span class="identifier">CLASS</span><span class="special">)</span></code>, <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_BEG</span><span class="special">(</span><span class="identifier">T1</span><span class="special">)</span></code> and <code class="computeroutput"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_END</span></code>
438            are used for this purpose. E.g.
439          </p>
440<pre class="programlisting"><span class="identifier">BOOST_THREAD_DCL_MOVABLE</span><span class="special">(</span><span class="identifier">thread</span><span class="special">)</span>
441</pre>
442<p>
443            and
444          </p>
445<pre class="programlisting"><span class="identifier">BOOST_THREAD_DCL_MOVABLE_BEG</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span> <span class="identifier">promise</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">BOOST_THREAD_DCL_MOVABLE_END</span>
446</pre>
447</div>
448</div>
449</div>
450<div class="section">
451<div class="titlepage"><div><div><h3 class="title">
452<a name="thread.emulations.bool_explicit_conversion"></a><a class="link" href="emulations.html#thread.emulations.bool_explicit_conversion" title="Bool explicit conversion">Bool explicit
453      conversion</a>
454</h3></div></div></div>
455<div class="toc"><dl class="toc">
456<dt><span class="section"><a href="emulations.html#thread.emulations.bool_explicit_conversion.bool_conversion"><code class="computeroutput"><span class="keyword">operator</span> </code><span class="emphasis"><em>unspecified-bool-type</em></span><code class="computeroutput"><span class="special">()</span> <span class="keyword">const</span></code></a></span></dt>
457<dt><span class="section"><a href="emulations.html#thread.emulations.bool_explicit_conversion.operator_not"><code class="computeroutput"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span></code></a></span></dt>
458</dl></div>
459<p>
460        Locks provide an explicit bool conversion operator when the compiler provides
461        them.
462      </p>
463<pre class="programlisting"><span class="keyword">explicit</span> <span class="keyword">operator</span> <span class="keyword">bool</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
464</pre>
465<p>
466        The library provides un implicit conversion to an undefined type that can
467        be used as a conditional expression.
468      </p>
469<pre class="programlisting"><span class="preprocessor">#if</span> <span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS</span><span class="special">)</span>
470    <span class="keyword">operator</span> <span class="emphasis"><em>unspecified-bool-type</em></span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
471    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span><span class="special">;</span>
472<span class="preprocessor">#else</span>
473    <span class="keyword">explicit</span> <span class="keyword">operator</span> <span class="keyword">bool</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
474<span class="preprocessor">#endif</span>
475</pre>
476<p>
477        The user should use the lock.owns_lock() when an explicit conversion is required.
478      </p>
479<div class="section">
480<div class="titlepage"><div><div><h4 class="title">
481<a name="thread.emulations.bool_explicit_conversion.bool_conversion"></a><a class="link" href="emulations.html#thread.emulations.bool_explicit_conversion.bool_conversion" title="operator unspecified-bool-type() const"><code class="computeroutput"><span class="keyword">operator</span> </code><span class="emphasis"><em>unspecified-bool-type</em></span><code class="computeroutput"><span class="special">()</span> <span class="keyword">const</span></code></a>
482</h4></div></div></div>
483<div class="variablelist">
484<p class="title"><b></b></p>
485<dl class="variablelist">
486<dt><span class="term">Returns:</span></dt>
487<dd><p>
488                If <a class="link" href="synchronization.html#thread.synchronization.locks.unique_lock.owns_lock" title="bool owns_lock() const"><code class="computeroutput"><span class="identifier">owns_lock</span><span class="special">()</span></code></a>
489                would return <code class="computeroutput"><span class="keyword">true</span></code>, a
490                value that evaluates to <code class="computeroutput"><span class="keyword">true</span></code>
491                in boolean contexts, otherwise a value that evaluates to <code class="computeroutput"><span class="keyword">false</span></code> in boolean contexts.
492              </p></dd>
493<dt><span class="term">Throws:</span></dt>
494<dd><p>
495                Nothing.
496              </p></dd>
497</dl>
498</div>
499</div>
500<div class="section">
501<div class="titlepage"><div><div><h4 class="title">
502<a name="thread.emulations.bool_explicit_conversion.operator_not"></a><a class="link" href="emulations.html#thread.emulations.bool_explicit_conversion.operator_not" title="bool operator!() const"><code class="computeroutput"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span></code></a>
503</h4></div></div></div>
504<div class="variablelist">
505<p class="title"><b></b></p>
506<dl class="variablelist">
507<dt><span class="term">Returns:</span></dt>
508<dd><p>
509                <code class="computeroutput"><span class="special">!</span></code> <a class="link" href="synchronization.html#thread.synchronization.locks.unique_lock.owns_lock" title="bool owns_lock() const"><code class="computeroutput"><span class="identifier">owns_lock</span><span class="special">()</span></code></a>.
510              </p></dd>
511<dt><span class="term">Throws:</span></dt>
512<dd><p>
513                Nothing.
514              </p></dd>
515</dl>
516</div>
517</div>
518</div>
519<div class="section">
520<div class="titlepage"><div><div><h3 class="title">
521<a name="thread.emulations.scoped_enums"></a><a class="link" href="emulations.html#thread.emulations.scoped_enums" title="Scoped Enums">Scoped Enums</a>
522</h3></div></div></div>
523<p>
524        Some of the enumerations defined in the standard library are scoped enums.
525      </p>
526<p>
527        On compilers that don't support them, the library uses a class to wrap the
528        underlying type. Instead of
529      </p>
530<pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">future_errc</span>
531<span class="special">{</span>
532    <span class="identifier">broken_promise</span><span class="special">,</span>
533    <span class="identifier">future_already_retrieved</span><span class="special">,</span>
534    <span class="identifier">promise_already_satisfied</span><span class="special">,</span>
535    <span class="identifier">no_state</span>
536<span class="special">};</span>
537</pre>
538<p>
539        the library declare these types as
540      </p>
541<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_DECLARE_BEGIN</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span>
542<span class="special">{</span>
543    <span class="identifier">broken_promise</span><span class="special">,</span>
544    <span class="identifier">future_already_retrieved</span><span class="special">,</span>
545    <span class="identifier">promise_already_satisfied</span><span class="special">,</span>
546    <span class="identifier">no_state</span>
547<span class="special">}</span>
548<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span>
549</pre>
550<p>
551        These macros allows to use 'future_errc' in almost all the cases as a scoped
552        enum.
553      </p>
554<p>
555        There are however some limitations:
556      </p>
557<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
558<li class="listitem">
559            The type is not a C++ enum, so 'is_enum&lt;future_errc&gt;' will be false_type.
560          </li>
561<li class="listitem">
562            The emulated scoped enum can not be used in switch nor in template arguments.
563            For these cases the user needs to use some macros.
564          </li>
565</ul></div>
566<p>
567        Instead of
568      </p>
569<pre class="programlisting">    <span class="keyword">switch</span> <span class="special">(</span><span class="identifier">ev</span><span class="special">)</span>
570    <span class="special">{</span>
571    <span class="keyword">case</span> <span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span><span class="special">:</span>
572<span class="comment">// ...</span>
573</pre>
574<p>
575        use
576      </p>
577<pre class="programlisting"><span class="keyword">switch</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">native_value</span><span class="special">(</span><span class="identifier">ev</span><span class="special">))</span>
578<span class="special">{</span>
579<span class="keyword">case</span> <span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span><span class="special">:</span>
580</pre>
581<p>
582        And instead of
583      </p>
584<pre class="programlisting"><span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_NO_CXX11_SCOPED_ENUMS</span>
585<span class="keyword">template</span> <span class="special">&lt;&gt;</span>
586<span class="keyword">struct</span> <span class="identifier">BOOST_SYMBOL_VISIBLE</span> <span class="identifier">is_error_code_enum</span><span class="special">&lt;</span><span class="identifier">future_errc</span><span class="special">&gt;</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">true_type</span> <span class="special">{</span> <span class="special">};</span>
587<span class="preprocessor">#endif</span>
588</pre>
589<p>
590        use
591      </p>
592<pre class="programlisting"><span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_NO_CXX11_SCOPED_ENUMS</span>
593<span class="keyword">template</span> <span class="special">&lt;&gt;</span>
594<span class="keyword">struct</span> <span class="identifier">BOOST_SYMBOL_VISIBLE</span> <span class="identifier">is_error_code_enum</span><span class="special">&lt;</span><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">enum_type</span><span class="special">&gt;</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">true_type</span> <span class="special">{</span> <span class="special">};</span>
595<span class="preprocessor">#endif</span>
596</pre>
597</div>
598</div>
599<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
600<td align="left"></td>
601<td align="right"><div class="copyright-footer">Copyright © 2007 -11 Anthony Williams<br>Copyright © 2011 -17 Vicente J. Botet Escriba<p>
602        Distributed under the Boost Software License, Version 1.0. (See accompanying
603        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
604      </p>
605</div></td>
606</tr></table>
607<hr>
608<div class="spirit-nav">
609<a accesskey="p" href="time.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../thread.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
610</div>
611</body>
612</html>
613