Imported Upstream version 1.64.0
[platform/upstream/boost.git] / doc / html / atomic / interface.html
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=US-ASCII">
5 <title>Programming interfaces</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="../atomic.html" title="Chapter&#160;7.&#160;Boost.Atomic">
10 <link rel="prev" href="thread_coordination.html" title="Thread coordination using Boost.Atomic">
11 <link rel="next" href="usage_examples.html" title="Usage examples">
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="thread_coordination.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../atomic.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="usage_examples.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="atomic.interface"></a><a class="link" href="interface.html" title="Programming interfaces">Programming interfaces</a>
29 </h2></div></div></div>
30 <div class="toc"><dl class="toc">
31 <dt><span class="section"><a href="interface.html#atomic.interface.configuration">Configuration and building</a></span></dt>
32 <dt><span class="section"><a href="interface.html#atomic.interface.interface_memory_order">Memory order</a></span></dt>
33 <dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object">Atomic objects</a></span></dt>
34 <dt><span class="section"><a href="interface.html#atomic.interface.interface_fences">Fences</a></span></dt>
35 <dt><span class="section"><a href="interface.html#atomic.interface.feature_macros">Feature testing macros</a></span></dt>
36 </dl></div>
37 <div class="section">
38 <div class="titlepage"><div><div><h3 class="title">
39 <a name="atomic.interface.configuration"></a><a class="link" href="interface.html#atomic.interface.configuration" title="Configuration and building">Configuration and building</a>
40 </h3></div></div></div>
41 <p>
42         The library contains header-only and compiled parts. The library is header-only
43         for lock-free cases but requires a separate binary to implement the lock-based
44         emulation. Users are able to detect whether linking to the compiled part
45         is required by checking the <a class="link" href="interface.html#atomic.interface.feature_macros" title="Feature testing macros">feature
46         macros</a>.
47       </p>
48 <p>
49         The following macros affect library behavior:
50       </p>
51 <div class="informaltable"><table class="table">
52 <colgroup>
53 <col>
54 <col>
55 </colgroup>
56 <thead><tr>
57 <th>
58                 <p>
59                   Macro
60                 </p>
61               </th>
62 <th>
63                 <p>
64                   Description
65                 </p>
66               </th>
67 </tr></thead>
68 <tbody>
69 <tr>
70 <td>
71                 <p>
72                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_CMPXCHG8B</span></code>
73                 </p>
74               </td>
75 <td>
76                 <p>
77                   Affects 32-bit x86 Oracle Studio builds. When defined, the library
78                   assumes the target CPU does not support <code class="computeroutput"><span class="identifier">cmpxchg8b</span></code>
79                   instruction used to support 64-bit atomic operations. This is the
80                   case with very old CPUs (pre-Pentium). The library does not perform
81                   runtime detection of this instruction, so running the code that
82                   uses 64-bit atomics on such CPUs will result in crashes, unless
83                   this macro is defined. Note that the macro does not affect MSVC,
84                   GCC and compatible compilers because the library infers this information
85                   from the compiler-defined macros.
86                 </p>
87               </td>
88 </tr>
89 <tr>
90 <td>
91                 <p>
92                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_CMPXCHG16B</span></code>
93                 </p>
94               </td>
95 <td>
96                 <p>
97                   Affects 64-bit x86 MSVC and Oracle Studio builds. When defined,
98                   the library assumes the target CPU does not support <code class="computeroutput"><span class="identifier">cmpxchg16b</span></code> instruction used to
99                   support 128-bit atomic operations. This is the case with some early
100                   64-bit AMD CPUs, all Intel CPUs and current AMD CPUs support this
101                   instruction. The library does not perform runtime detection of
102                   this instruction, so running the code that uses 128-bit atomics
103                   on such CPUs will result in crashes, unless this macro is defined.
104                   Note that the macro does not affect GCC and compatible compilers
105                   because the library infers this information from the compiler-defined
106                   macros.
107                 </p>
108               </td>
109 </tr>
110 <tr>
111 <td>
112                 <p>
113                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_MFENCE</span></code>
114                 </p>
115               </td>
116 <td>
117                 <p>
118                   Affects 32-bit x86 Oracle Studio builds. When defined, the library
119                   assumes the target CPU does not support <code class="computeroutput"><span class="identifier">mfence</span></code>
120                   instruction used to implement thread fences. This instruction was
121                   added with SSE2 instruction set extension, which was available
122                   in CPUs since Intel Pentium 4. The library does not perform runtime
123                   detection of this instruction, so running the library code on older
124                   CPUs will result in crashes, unless this macro is defined. Note
125                   that the macro does not affect MSVC, GCC and compatible compilers
126                   because the library infers this information from the compiler-defined
127                   macros.
128                 </p>
129               </td>
130 </tr>
131 <tr>
132 <td>
133                 <p>
134                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FORCE_FALLBACK</span></code>
135                 </p>
136               </td>
137 <td>
138                 <p>
139                   When defined, all operations are implemented with locks. This is
140                   mostly used for testing and should not be used in real world projects.
141                 </p>
142               </td>
143 </tr>
144 <tr>
145 <td>
146                 <p>
147                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_DYN_LINK</span></code>
148                   and <code class="computeroutput"><span class="identifier">BOOST_ALL_DYN_LINK</span></code>
149                 </p>
150               </td>
151 <td>
152                 <p>
153                   Control library linking. If defined, the library assumes dynamic
154                   linking, otherwise static. The latter macro affects all Boost libraries,
155                   not just <span class="bold"><strong>Boost.Atomic</strong></span>.
156                 </p>
157               </td>
158 </tr>
159 <tr>
160 <td>
161                 <p>
162                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_LIB</span></code>
163                   and <code class="computeroutput"><span class="identifier">BOOST_ALL_NO_LIB</span></code>
164                 </p>
165               </td>
166 <td>
167                 <p>
168                   Control library auto-linking on Windows. When defined, disables
169                   auto-linking. The latter macro affects all Boost libraries, not
170                   just <span class="bold"><strong>Boost.Atomic</strong></span>.
171                 </p>
172               </td>
173 </tr>
174 </tbody>
175 </table></div>
176 <p>
177         Besides macros, it is important to specify the correct compiler options for
178         the target CPU. With GCC and compatible compilers this affects whether particular
179         atomic operations are lock-free or not.
180       </p>
181 <p>
182         Boost building process is described in the <a href="http://www.boost.org/doc/libs/release/more/getting_started/" target="_top">Getting
183         Started guide</a>. For example, you can build <span class="bold"><strong>Boost.Atomic</strong></span>
184         with the following command line:
185       </p>
186 <pre class="programlisting">bjam --with-atomic variant=release instruction-set=core2 stage
187 </pre>
188 </div>
189 <div class="section">
190 <div class="titlepage"><div><div><h3 class="title">
191 <a name="atomic.interface.interface_memory_order"></a><a class="link" href="interface.html#atomic.interface.interface_memory_order" title="Memory order">Memory order</a>
192 </h3></div></div></div>
193 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">memory_order</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
194 </pre>
195 <p>
196         The enumeration <code class="literal">boost::memory_order</code> defines the following
197         values to represent memory ordering constraints:
198       </p>
199 <div class="informaltable"><table class="table">
200 <colgroup>
201 <col>
202 <col>
203 </colgroup>
204 <thead><tr>
205 <th>
206                 <p>
207                   Constant
208                 </p>
209               </th>
210 <th>
211                 <p>
212                   Description
213                 </p>
214               </th>
215 </tr></thead>
216 <tbody>
217 <tr>
218 <td>
219                 <p>
220                   <code class="computeroutput"><span class="identifier">memory_order_relaxed</span></code>
221                 </p>
222               </td>
223 <td>
224                 <p>
225                   No ordering constraint. Informally speaking, following operations
226                   may be reordered before, preceding operations may be reordered
227                   after the atomic operation. This constraint is suitable only when
228                   either a) further operations do not depend on the outcome of the
229                   atomic operation or b) ordering is enforced through stand-alone
230                   <code class="computeroutput"><span class="identifier">atomic_thread_fence</span></code>
231                   operations. The operation on the atomic value itself is still atomic
232                   though.
233                 </p>
234               </td>
235 </tr>
236 <tr>
237 <td>
238                 <p>
239                   <code class="computeroutput"><span class="identifier">memory_order_release</span></code>
240                 </p>
241               </td>
242 <td>
243                 <p>
244                   Perform <code class="computeroutput"><span class="identifier">release</span></code>
245                   operation. Informally speaking, prevents all preceding memory operations
246                   to be reordered past this point.
247                 </p>
248               </td>
249 </tr>
250 <tr>
251 <td>
252                 <p>
253                   <code class="computeroutput"><span class="identifier">memory_order_acquire</span></code>
254                 </p>
255               </td>
256 <td>
257                 <p>
258                   Perform <code class="computeroutput"><span class="identifier">acquire</span></code>
259                   operation. Informally speaking, prevents succeeding memory operations
260                   to be reordered before this point.
261                 </p>
262               </td>
263 </tr>
264 <tr>
265 <td>
266                 <p>
267                   <code class="computeroutput"><span class="identifier">memory_order_consume</span></code>
268                 </p>
269               </td>
270 <td>
271                 <p>
272                   Perform <code class="computeroutput"><span class="identifier">consume</span></code>
273                   operation. More relaxed (and on some architectures more efficient)
274                   than <code class="computeroutput"><span class="identifier">memory_order_acquire</span></code>
275                   as it only affects succeeding operations that are computationally-dependent
276                   on the value retrieved from an atomic variable.
277                 </p>
278               </td>
279 </tr>
280 <tr>
281 <td>
282                 <p>
283                   <code class="computeroutput"><span class="identifier">memory_order_acq_rel</span></code>
284                 </p>
285               </td>
286 <td>
287                 <p>
288                   Perform both <code class="computeroutput"><span class="identifier">release</span></code>
289                   and <code class="computeroutput"><span class="identifier">acquire</span></code> operation
290                 </p>
291               </td>
292 </tr>
293 <tr>
294 <td>
295                 <p>
296                   <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code>
297                 </p>
298               </td>
299 <td>
300                 <p>
301                   Enforce sequential consistency. Implies <code class="computeroutput"><span class="identifier">memory_order_acq_rel</span></code>,
302                   but additionally enforces total order for all operations such qualified.
303                 </p>
304               </td>
305 </tr>
306 </tbody>
307 </table></div>
308 <p>
309         See section <a class="link" href="thread_coordination.html" title="Thread coordination using Boost.Atomic"><span class="emphasis"><em>happens-before</em></span></a>
310         for explanation of the various ordering constraints.
311       </p>
312 </div>
313 <div class="section">
314 <div class="titlepage"><div><div><h3 class="title">
315 <a name="atomic.interface.interface_atomic_object"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object" title="Atomic objects">Atomic objects</a>
316 </h3></div></div></div>
317 <div class="toc"><dl class="toc">
318 <dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_generic"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
319         template class</a></span></dt>
320 <dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_integral"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>integral</em></span>&gt;</code>
321         template class</a></span></dt>
322 <dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_pointer"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>pointer</em></span>&gt;</code>
323         template class</a></span></dt>
324 </dl></div>
325 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
326 </pre>
327 <p>
328         <code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code> provides methods
329         for atomically accessing variables of a suitable type <code class="literal"><span class="emphasis"><em>T</em></span></code>.
330         The type is suitable if it is <span class="emphasis"><em>trivially copyable</em></span> (3.9/9
331         [basic.types]). Following are examples of the types compatible with this
332         requirement:
333       </p>
334 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
335 <li class="listitem">
336             a scalar type (e.g. integer, boolean, enum or pointer type)
337           </li>
338 <li class="listitem">
339             a <code class="literal">class</code> or <code class="literal">struct</code> that has no non-trivial
340             copy or move constructors or assignment operators, has a trivial destructor,
341             and that is comparable via <code class="literal">memcmp</code>.
342           </li>
343 </ul></div>
344 <p>
345         Note that classes with virtual functions or virtual base classes do not satisfy
346         the requirements. Also be warned that structures with "padding"
347         between data members may compare non-equal via <code class="literal">memcmp</code>
348         even though all members are equal.
349       </p>
350 <div class="section">
351 <div class="titlepage"><div><div><h4 class="title">
352 <a name="atomic.interface.interface_atomic_object.interface_atomic_generic"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_generic" title="boost::atomic&lt;T&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
353         template class</a>
354 </h4></div></div></div>
355 <p>
356           All atomic objects supports the following operations:
357         </p>
358 <div class="informaltable"><table class="table">
359 <colgroup>
360 <col>
361 <col>
362 </colgroup>
363 <thead><tr>
364 <th>
365                   <p>
366                     Syntax
367                   </p>
368                 </th>
369 <th>
370                   <p>
371                     Description
372                   </p>
373                 </th>
374 </tr></thead>
375 <tbody>
376 <tr>
377 <td>
378                   <p>
379                     <code class="computeroutput"><span class="identifier">atomic</span><span class="special">()</span></code>
380                   </p>
381                 </td>
382 <td>
383                   <p>
384                     Initialize to an unspecified value
385                   </p>
386                 </td>
387 </tr>
388 <tr>
389 <td>
390                   <p>
391                     <code class="computeroutput"><span class="identifier">atomic</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">initial_value</span><span class="special">)</span></code>
392                   </p>
393                 </td>
394 <td>
395                   <p>
396                     Initialize to <code class="literal">initial_value</code>
397                   </p>
398                 </td>
399 </tr>
400 <tr>
401 <td>
402                   <p>
403                     <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">is_lock_free</span><span class="special">()</span></code>
404                   </p>
405                 </td>
406 <td>
407                   <p>
408                     Checks if the atomic object is lock-free
409                   </p>
410                 </td>
411 </tr>
412 <tr>
413 <td>
414                   <p>
415                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">load</span><span class="special">(</span><span class="identifier">memory_order</span>
416                     <span class="identifier">order</span><span class="special">)</span></code>
417                   </p>
418                 </td>
419 <td>
420                   <p>
421                     Return current value
422                   </p>
423                 </td>
424 </tr>
425 <tr>
426 <td>
427                   <p>
428                     <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">store</span><span class="special">(</span><span class="identifier">T</span>
429                     <span class="identifier">value</span><span class="special">,</span>
430                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
431                   </p>
432                 </td>
433 <td>
434                   <p>
435                     Write new value to atomic variable
436                   </p>
437                 </td>
438 </tr>
439 <tr>
440 <td>
441                   <p>
442                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span>
443                     <span class="identifier">new_value</span><span class="special">,</span>
444                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
445                   </p>
446                 </td>
447 <td>
448                   <p>
449                     Exchange current value with <code class="computeroutput"><span class="identifier">new_value</span></code>,
450                     returning current value
451                   </p>
452                 </td>
453 </tr>
454 <tr>
455 <td>
456                   <p>
457                     <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_weak</span><span class="special">(</span><span class="identifier">T</span>
458                     <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
459                     <span class="identifier">desired</span><span class="special">,</span>
460                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
461                   </p>
462                 </td>
463 <td>
464                   <p>
465                     Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
466                     change it to <code class="computeroutput"><span class="identifier">desired</span></code>
467                     if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
468                     if an exchange has been performed, and always writes the previous
469                     value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
470                     May fail spuriously, so must generally be retried in a loop.
471                   </p>
472                 </td>
473 </tr>
474 <tr>
475 <td>
476                   <p>
477                     <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_weak</span><span class="special">(</span><span class="identifier">T</span>
478                     <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
479                     <span class="identifier">desired</span><span class="special">,</span>
480                     <span class="identifier">memory_order</span> <span class="identifier">success_order</span><span class="special">,</span> <span class="identifier">memory_order</span>
481                     <span class="identifier">failure_order</span><span class="special">)</span></code>
482                   </p>
483                 </td>
484 <td>
485                   <p>
486                     Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
487                     change it to <code class="computeroutput"><span class="identifier">desired</span></code>
488                     if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
489                     if an exchange has been performed, and always writes the previous
490                     value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
491                     May fail spuriously, so must generally be retried in a loop.
492                   </p>
493                 </td>
494 </tr>
495 <tr>
496 <td>
497                   <p>
498                     <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_strong</span><span class="special">(</span><span class="identifier">T</span>
499                     <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
500                     <span class="identifier">desired</span><span class="special">,</span>
501                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
502                   </p>
503                 </td>
504 <td>
505                   <p>
506                     Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
507                     change it to <code class="computeroutput"><span class="identifier">desired</span></code>
508                     if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
509                     if an exchange has been performed, and always writes the previous
510                     value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
511                   </p>
512                 </td>
513 </tr>
514 <tr>
515 <td>
516                   <p>
517                     <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_strong</span><span class="special">(</span><span class="identifier">T</span>
518                     <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
519                     <span class="identifier">desired</span><span class="special">,</span>
520                     <span class="identifier">memory_order</span> <span class="identifier">success_order</span><span class="special">,</span> <span class="identifier">memory_order</span>
521                     <span class="identifier">failure_order</span><span class="special">))</span></code>
522                   </p>
523                 </td>
524 <td>
525                   <p>
526                     Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
527                     change it to <code class="computeroutput"><span class="identifier">desired</span></code>
528                     if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
529                     if an exchange has been performed, and always writes the previous
530                     value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
531                   </p>
532                 </td>
533 </tr>
534 </tbody>
535 </table></div>
536 <p>
537           <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
538         </p>
539 <p>
540           The <code class="computeroutput"><span class="identifier">compare_exchange_weak</span></code>/<code class="computeroutput"><span class="identifier">compare_exchange_strong</span></code> variants taking
541           four parameters differ from the three parameter variants in that they allow
542           a different memory ordering constraint to be specified in case the operation
543           fails.
544         </p>
545 <p>
546           In addition to these explicit operations, each <code class="literal">atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
547           object also supports implicit <code class="literal">store</code> and <code class="literal">load</code>
548           through the use of "assignment" and "conversion to <code class="literal">T</code>"
549           operators. Avoid using these operators, as they do not allow explicit specification
550           of a memory ordering constraint.
551         </p>
552 </div>
553 <div class="section">
554 <div class="titlepage"><div><div><h4 class="title">
555 <a name="atomic.interface.interface_atomic_object.interface_atomic_integral"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_integral" title="boost::atomic&lt;integral&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>integral</em></span>&gt;</code>
556         template class</a>
557 </h4></div></div></div>
558 <p>
559           In addition to the operations listed in the previous section, <code class="literal">boost::atomic&lt;<span class="emphasis"><em>I</em></span>&gt;</code>
560           for integral types <code class="literal"><span class="emphasis"><em>I</em></span></code> supports the
561           following operations:
562         </p>
563 <div class="informaltable"><table class="table">
564 <colgroup>
565 <col>
566 <col>
567 </colgroup>
568 <thead><tr>
569 <th>
570                   <p>
571                     Syntax
572                   </p>
573                 </th>
574 <th>
575                   <p>
576                     Description
577                   </p>
578                 </th>
579 </tr></thead>
580 <tbody>
581 <tr>
582 <td>
583                   <p>
584                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_add</span><span class="special">(</span><span class="identifier">T</span>
585                     <span class="identifier">v</span><span class="special">,</span>
586                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
587                   </p>
588                 </td>
589 <td>
590                   <p>
591                     Add <code class="computeroutput"><span class="identifier">v</span></code> to variable,
592                     returning previous value
593                   </p>
594                 </td>
595 </tr>
596 <tr>
597 <td>
598                   <p>
599                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_sub</span><span class="special">(</span><span class="identifier">T</span>
600                     <span class="identifier">v</span><span class="special">,</span>
601                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
602                   </p>
603                 </td>
604 <td>
605                   <p>
606                     Subtract <code class="computeroutput"><span class="identifier">v</span></code> from
607                     variable, returning previous value
608                   </p>
609                 </td>
610 </tr>
611 <tr>
612 <td>
613                   <p>
614                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_and</span><span class="special">(</span><span class="identifier">T</span>
615                     <span class="identifier">v</span><span class="special">,</span>
616                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
617                   </p>
618                 </td>
619 <td>
620                   <p>
621                     Apply bit-wise "and" with <code class="computeroutput"><span class="identifier">v</span></code>
622                     to variable, returning previous value
623                   </p>
624                 </td>
625 </tr>
626 <tr>
627 <td>
628                   <p>
629                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_or</span><span class="special">(</span><span class="identifier">T</span>
630                     <span class="identifier">v</span><span class="special">,</span>
631                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
632                   </p>
633                 </td>
634 <td>
635                   <p>
636                     Apply bit-wise "or" with <code class="computeroutput"><span class="identifier">v</span></code>
637                     to variable, returning previous value
638                   </p>
639                 </td>
640 </tr>
641 <tr>
642 <td>
643                   <p>
644                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_xor</span><span class="special">(</span><span class="identifier">T</span>
645                     <span class="identifier">v</span><span class="special">,</span>
646                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
647                   </p>
648                 </td>
649 <td>
650                   <p>
651                     Apply bit-wise "xor" with <code class="computeroutput"><span class="identifier">v</span></code>
652                     to variable, returning previous value
653                   </p>
654                 </td>
655 </tr>
656 </tbody>
657 </table></div>
658 <p>
659           <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
660         </p>
661 <p>
662           In addition to these explicit operations, each <code class="literal">boost::atomic&lt;<span class="emphasis"><em>I</em></span>&gt;</code>
663           object also supports implicit pre-/post- increment/decrement, as well as
664           the operators <code class="computeroutput"><span class="special">+=</span></code>, <code class="computeroutput"><span class="special">-=</span></code>, <code class="computeroutput"><span class="special">&amp;=</span></code>,
665           <code class="computeroutput"><span class="special">|=</span></code> and <code class="computeroutput"><span class="special">^=</span></code>.
666           Avoid using these operators, as they do not allow explicit specification
667           of a memory ordering constraint.
668         </p>
669 </div>
670 <div class="section">
671 <div class="titlepage"><div><div><h4 class="title">
672 <a name="atomic.interface.interface_atomic_object.interface_atomic_pointer"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_pointer" title="boost::atomic&lt;pointer&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>pointer</em></span>&gt;</code>
673         template class</a>
674 </h4></div></div></div>
675 <p>
676           In addition to the operations applicable to all atomic object, <code class="literal">boost::atomic&lt;<span class="emphasis"><em>P</em></span>&gt;</code>
677           for pointer types <code class="literal"><span class="emphasis"><em>P</em></span></code> (other than
678           <code class="literal">void</code> pointers) support the following operations:
679         </p>
680 <div class="informaltable"><table class="table">
681 <colgroup>
682 <col>
683 <col>
684 </colgroup>
685 <thead><tr>
686 <th>
687                   <p>
688                     Syntax
689                   </p>
690                 </th>
691 <th>
692                   <p>
693                     Description
694                   </p>
695                 </th>
696 </tr></thead>
697 <tbody>
698 <tr>
699 <td>
700                   <p>
701                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_add</span><span class="special">(</span><span class="identifier">ptrdiff_t</span>
702                     <span class="identifier">v</span><span class="special">,</span>
703                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
704                   </p>
705                 </td>
706 <td>
707                   <p>
708                     Add <code class="computeroutput"><span class="identifier">v</span></code> to variable,
709                     returning previous value
710                   </p>
711                 </td>
712 </tr>
713 <tr>
714 <td>
715                   <p>
716                     <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_sub</span><span class="special">(</span><span class="identifier">ptrdiff_t</span>
717                     <span class="identifier">v</span><span class="special">,</span>
718                     <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
719                   </p>
720                 </td>
721 <td>
722                   <p>
723                     Subtract <code class="computeroutput"><span class="identifier">v</span></code> from
724                     variable, returning previous value
725                   </p>
726                 </td>
727 </tr>
728 </tbody>
729 </table></div>
730 <p>
731           <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
732         </p>
733 <p>
734           In addition to these explicit operations, each <code class="literal">boost::atomic&lt;<span class="emphasis"><em>P</em></span>&gt;</code>
735           object also supports implicit pre-/post- increment/decrement, as well as
736           the operators <code class="computeroutput"><span class="special">+=</span></code>, <code class="computeroutput"><span class="special">-=</span></code>. Avoid using these operators, as they
737           do not allow explicit specification of a memory ordering constraint.
738         </p>
739 </div>
740 </div>
741 <div class="section">
742 <div class="titlepage"><div><div><h3 class="title">
743 <a name="atomic.interface.interface_fences"></a><a class="link" href="interface.html#atomic.interface.interface_fences" title="Fences">Fences</a>
744 </h3></div></div></div>
745 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">fences</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
746 </pre>
747 <div class="informaltable"><table class="table">
748 <colgroup>
749 <col>
750 <col>
751 </colgroup>
752 <thead><tr>
753 <th>
754                 <p>
755                   Syntax
756                 </p>
757               </th>
758 <th>
759                 <p>
760                   Description
761                 </p>
762               </th>
763 </tr></thead>
764 <tbody>
765 <tr>
766 <td>
767                 <p>
768                   <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">atomic_thread_fence</span><span class="special">(</span><span class="identifier">memory_order</span>
769                   <span class="identifier">order</span><span class="special">)</span></code>
770                 </p>
771               </td>
772 <td>
773                 <p>
774                   Issue fence for coordination with other threads.
775                 </p>
776               </td>
777 </tr>
778 <tr>
779 <td>
780                 <p>
781                   <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">atomic_signal_fence</span><span class="special">(</span><span class="identifier">memory_order</span>
782                   <span class="identifier">order</span><span class="special">)</span></code>
783                 </p>
784               </td>
785 <td>
786                 <p>
787                   Issue fence for coordination with signal handler (only in same
788                   thread).
789                 </p>
790               </td>
791 </tr>
792 </tbody>
793 </table></div>
794 </div>
795 <div class="section">
796 <div class="titlepage"><div><div><h3 class="title">
797 <a name="atomic.interface.feature_macros"></a><a class="link" href="interface.html#atomic.interface.feature_macros" title="Feature testing macros">Feature testing macros</a>
798 </h3></div></div></div>
799 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">capabilities</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
800 </pre>
801 <p>
802         <span class="bold"><strong>Boost.Atomic</strong></span> defines a number of macros
803         to allow compile-time detection whether an atomic data type is implemented
804         using "true" atomic operations, or whether an internal "lock"
805         is used to provide atomicity. The following macros will be defined to <code class="computeroutput"><span class="number">0</span></code> if operations on the data type always require
806         a lock, to <code class="computeroutput"><span class="number">1</span></code> if operations on
807         the data type may sometimes require a lock, and to <code class="computeroutput"><span class="number">2</span></code>
808         if they are always lock-free:
809       </p>
810 <div class="informaltable"><table class="table">
811 <colgroup>
812 <col>
813 <col>
814 </colgroup>
815 <thead><tr>
816 <th>
817                 <p>
818                   Macro
819                 </p>
820               </th>
821 <th>
822                 <p>
823                   Description
824                 </p>
825               </th>
826 </tr></thead>
827 <tbody>
828 <tr>
829 <td>
830                 <p>
831                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FLAG_LOCK_FREE</span></code>
832                 </p>
833               </td>
834 <td>
835                 <p>
836                   Indicate whether <code class="computeroutput"><span class="identifier">atomic_flag</span></code>
837                   is lock-free
838                 </p>
839               </td>
840 </tr>
841 <tr>
842 <td>
843                 <p>
844                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_BOOL_LOCK_FREE</span></code>
845                 </p>
846               </td>
847 <td>
848                 <p>
849                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code> is lock-free
850                 </p>
851               </td>
852 </tr>
853 <tr>
854 <td>
855                 <p>
856                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR_LOCK_FREE</span></code>
857                 </p>
858               </td>
859 <td>
860                 <p>
861                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span></code> (including signed/unsigned
862                   variants) is lock-free
863                 </p>
864               </td>
865 </tr>
866 <tr>
867 <td>
868                 <p>
869                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR16_T_LOCK_FREE</span></code>
870                 </p>
871               </td>
872 <td>
873                 <p>
874                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char16_t</span><span class="special">&gt;</span></code> (including signed/unsigned
875                   variants) is lock-free
876                 </p>
877               </td>
878 </tr>
879 <tr>
880 <td>
881                 <p>
882                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR32_T_LOCK_FREE</span></code>
883                 </p>
884               </td>
885 <td>
886                 <p>
887                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char32_t</span><span class="special">&gt;</span></code> (including signed/unsigned
888                   variants) is lock-free
889                 </p>
890               </td>
891 </tr>
892 <tr>
893 <td>
894                 <p>
895                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_WCHAR_T_LOCK_FREE</span></code>
896                 </p>
897               </td>
898 <td>
899                 <p>
900                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">wchar_t</span><span class="special">&gt;</span></code> (including signed/unsigned
901                   variants) is lock-free
902                 </p>
903               </td>
904 </tr>
905 <tr>
906 <td>
907                 <p>
908                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_SHORT_LOCK_FREE</span></code>
909                 </p>
910               </td>
911 <td>
912                 <p>
913                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">short</span><span class="special">&gt;</span></code> (including signed/unsigned
914                   variants) is lock-free
915                 </p>
916               </td>
917 </tr>
918 <tr>
919 <td>
920                 <p>
921                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT_LOCK_FREE</span></code>
922                 </p>
923               </td>
924 <td>
925                 <p>
926                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code> (including signed/unsigned
927                   variants) is lock-free
928                 </p>
929               </td>
930 </tr>
931 <tr>
932 <td>
933                 <p>
934                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_LONG_LOCK_FREE</span></code>
935                 </p>
936               </td>
937 <td>
938                 <p>
939                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span></code> (including signed/unsigned
940                   variants) is lock-free
941                 </p>
942               </td>
943 </tr>
944 <tr>
945 <td>
946                 <p>
947                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_LLONG_LOCK_FREE</span></code>
948                 </p>
949               </td>
950 <td>
951                 <p>
952                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">long</span>
953                   <span class="keyword">long</span><span class="special">&gt;</span></code>
954                   (including signed/unsigned variants) is lock-free
955                 </p>
956               </td>
957 </tr>
958 <tr>
959 <td>
960                 <p>
961                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_ADDRESS_LOCK_FREE</span></code>
962                   or <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_POINTER_LOCK_FREE</span></code>
963                 </p>
964               </td>
965 <td>
966                 <p>
967                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">T</span>
968                   <span class="special">*&gt;</span></code> is lock-free
969                 </p>
970               </td>
971 </tr>
972 <tr>
973 <td>
974                 <p>
975                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_THREAD_FENCE</span></code>
976                 </p>
977               </td>
978 <td>
979                 <p>
980                   Indicate whether <code class="computeroutput"><span class="identifier">atomic_thread_fence</span></code>
981                   function is lock-free
982                 </p>
983               </td>
984 </tr>
985 <tr>
986 <td>
987                 <p>
988                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_SIGNAL_FENCE</span></code>
989                 </p>
990               </td>
991 <td>
992                 <p>
993                   Indicate whether <code class="computeroutput"><span class="identifier">atomic_signal_fence</span></code>
994                   function is lock-free
995                 </p>
996               </td>
997 </tr>
998 </tbody>
999 </table></div>
1000 <p>
1001         In addition to these standard macros, <span class="bold"><strong>Boost.Atomic</strong></span>
1002         also defines a number of extension macros, which can also be useful. Like
1003         the standard ones, these macros are defined to values <code class="computeroutput"><span class="number">0</span></code>,
1004         <code class="computeroutput"><span class="number">1</span></code> and <code class="computeroutput"><span class="number">2</span></code>
1005         to indicate whether the corresponding operations are lock-free or not.
1006       </p>
1007 <div class="informaltable"><table class="table">
1008 <colgroup>
1009 <col>
1010 <col>
1011 </colgroup>
1012 <thead><tr>
1013 <th>
1014                 <p>
1015                   Macro
1016                 </p>
1017               </th>
1018 <th>
1019                 <p>
1020                   Description
1021                 </p>
1022               </th>
1023 </tr></thead>
1024 <tbody>
1025 <tr>
1026 <td>
1027                 <p>
1028                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT8_LOCK_FREE</span></code>
1029                 </p>
1030               </td>
1031 <td>
1032                 <p>
1033                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int8_type</span><span class="special">&gt;</span></code> is lock-free.
1034                 </p>
1035               </td>
1036 </tr>
1037 <tr>
1038 <td>
1039                 <p>
1040                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT16_LOCK_FREE</span></code>
1041                 </p>
1042               </td>
1043 <td>
1044                 <p>
1045                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int16_type</span><span class="special">&gt;</span></code> is lock-free.
1046                 </p>
1047               </td>
1048 </tr>
1049 <tr>
1050 <td>
1051                 <p>
1052                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT32_LOCK_FREE</span></code>
1053                 </p>
1054               </td>
1055 <td>
1056                 <p>
1057                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int32_type</span><span class="special">&gt;</span></code> is lock-free.
1058                 </p>
1059               </td>
1060 </tr>
1061 <tr>
1062 <td>
1063                 <p>
1064                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT64_LOCK_FREE</span></code>
1065                 </p>
1066               </td>
1067 <td>
1068                 <p>
1069                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int64_type</span><span class="special">&gt;</span></code> is lock-free.
1070                 </p>
1071               </td>
1072 </tr>
1073 <tr>
1074 <td>
1075                 <p>
1076                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT128_LOCK_FREE</span></code>
1077                 </p>
1078               </td>
1079 <td>
1080                 <p>
1081                   Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int128_type</span><span class="special">&gt;</span></code> is lock-free.
1082                 </p>
1083               </td>
1084 </tr>
1085 <tr>
1086 <td>
1087                 <p>
1088                   <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT</span></code>
1089                 </p>
1090               </td>
1091 <td>
1092                 <p>
1093                   Defined after including <code class="computeroutput"><span class="identifier">atomic_flag</span><span class="special">.</span><span class="identifier">hpp</span></code>,
1094                   if the implementation does not support the <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FLAG_INIT</span></code>
1095                   macro for static initialization of <code class="computeroutput"><span class="identifier">atomic_flag</span></code>.
1096                   This macro is typically defined for pre-C++11 compilers.
1097                 </p>
1098               </td>
1099 </tr>
1100 </tbody>
1101 </table></div>
1102 <p>
1103         In the table above, <code class="computeroutput"><span class="identifier">intN_type</span></code>
1104         is a type that fits storage of contiguous <code class="computeroutput"><span class="identifier">N</span></code>
1105         bits, suitably aligned for atomic operations.
1106       </p>
1107 </div>
1108 </div>
1109 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
1110 <td align="left"></td>
1111 <td align="right"><div class="copyright-footer">Copyright &#169; 2011 Helge Bahmann<br>Copyright &#169; 2012 Tim Blechmann<br>Copyright &#169; 2013 Andrey Semashev<p>
1112         Distributed under the Boost Software License, Version 1.0. (See accompanying
1113         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>)
1114       </p>
1115 </div></td>
1116 </tr></table>
1117 <hr>
1118 <div class="spirit-nav">
1119 <a accesskey="p" href="thread_coordination.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../atomic.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="usage_examples.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
1120 </div>
1121 </body>
1122 </html>