Imported Upstream version 1.64.0
[platform/upstream/boost.git] / doc / html / atomic.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>Chapter&#160;7.&#160;Boost.Atomic</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="libraries.html" title="Part&#160;I.&#160;The Boost C++ Libraries (BoostBook Subset)">
10 <link rel="prev" href="array/ack.html" title="Acknowledgements">
11 <link rel="next" href="atomic/thread_coordination.html" title="Thread coordination using Boost.Atomic">
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="array/ack.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.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="atomic/thread_coordination.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
25 </div>
26 <div class="chapter">
27 <div class="titlepage"><div>
28 <div><h2 class="title">
29 <a name="atomic"></a>Chapter&#160;7.&#160;Boost.Atomic</h2></div>
30 <div><div class="author"><h3 class="author">
31 <span class="firstname">Helge</span> <span class="surname">Bahmann</span>
32 </h3></div></div>
33 <div><div class="author"><h3 class="author">
34 <span class="firstname">Andrey</span> <span class="surname">Semashev</span>
35 </h3></div></div>
36 <div><p class="copyright">Copyright &#169; 2011 Helge Bahmann</p></div>
37 <div><p class="copyright">Copyright &#169; 2012 Tim Blechmann</p></div>
38 <div><p class="copyright">Copyright &#169; 2013 Andrey Semashev</p></div>
39 <div><div class="legalnotice">
40 <a name="atomic.legal"></a><p>
41         Distributed under the Boost Software License, Version 1.0. (See accompanying
42         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>)
43       </p>
44 </div></div>
45 </div></div>
46 <div class="toc">
47 <p><b>Table of Contents</b></p>
48 <dl class="toc">
49 <dt><span class="section"><a href="atomic.html#atomic.introduction">Introduction</a></span></dt>
50 <dd><dl>
51 <dt><span class="section"><a href="atomic.html#atomic.introduction.introduction_presenting">Presenting
52       Boost.Atomic</a></span></dt>
53 <dt><span class="section"><a href="atomic.html#atomic.introduction.introduction_purpose">Purpose</a></span></dt>
54 </dl></dd>
55 <dt><span class="section"><a href="atomic/thread_coordination.html">Thread coordination using Boost.Atomic</a></span></dt>
56 <dd><dl>
57 <dt><span class="section"><a href="atomic/thread_coordination.html#atomic.thread_coordination.mutex">Enforcing <span class="emphasis"><em>happens-before</em></span>
58       through mutual exclusion</a></span></dt>
59 <dt><span class="section"><a href="atomic/thread_coordination.html#atomic.thread_coordination.release_acquire"><span class="emphasis"><em>happens-before</em></span>
60       through <code class="literal">release</code> and <code class="literal">acquire</code></a></span></dt>
61 <dt><span class="section"><a href="atomic/thread_coordination.html#atomic.thread_coordination.fences">Fences</a></span></dt>
62 <dt><span class="section"><a href="atomic/thread_coordination.html#atomic.thread_coordination.release_consume"><span class="emphasis"><em>happens-before</em></span>
63       through <code class="literal">release</code> and <code class="literal">consume</code></a></span></dt>
64 <dt><span class="section"><a href="atomic/thread_coordination.html#atomic.thread_coordination.seq_cst">Sequential consistency</a></span></dt>
65 </dl></dd>
66 <dt><span class="section"><a href="atomic/interface.html">Programming interfaces</a></span></dt>
67 <dd><dl>
68 <dt><span class="section"><a href="atomic/interface.html#atomic.interface.configuration">Configuration and building</a></span></dt>
69 <dt><span class="section"><a href="atomic/interface.html#atomic.interface.interface_memory_order">Memory order</a></span></dt>
70 <dt><span class="section"><a href="atomic/interface.html#atomic.interface.interface_atomic_object">Atomic objects</a></span></dt>
71 <dt><span class="section"><a href="atomic/interface.html#atomic.interface.interface_fences">Fences</a></span></dt>
72 <dt><span class="section"><a href="atomic/interface.html#atomic.interface.feature_macros">Feature testing macros</a></span></dt>
73 </dl></dd>
74 <dt><span class="section"><a href="atomic/usage_examples.html">Usage examples</a></span></dt>
75 <dd><dl>
76 <dt><span class="section"><a href="atomic/usage_examples.html#boost_atomic.usage_examples.example_reference_counters">Reference
77       counting</a></span></dt>
78 <dt><span class="section"><a href="atomic/usage_examples.html#boost_atomic.usage_examples.example_spinlock">Spinlock</a></span></dt>
79 <dt><span class="section"><a href="atomic/usage_examples.html#boost_atomic.usage_examples.singleton">Singleton with
80       double-checked locking pattern</a></span></dt>
81 <dt><span class="section"><a href="atomic/usage_examples.html#boost_atomic.usage_examples.example_ringbuffer">Wait-free
82       ring buffer</a></span></dt>
83 <dt><span class="section"><a href="atomic/usage_examples.html#boost_atomic.usage_examples.mp_queue">Wait-free multi-producer
84       queue</a></span></dt>
85 </dl></dd>
86 <dt><span class="section"><a href="atomic/limitations.html">Limitations</a></span></dt>
87 <dt><span class="section"><a href="atomic/porting.html">Porting</a></span></dt>
88 <dd><dl>
89 <dt><span class="section"><a href="atomic/porting.html#atomic.porting.unit_tests">Unit tests</a></span></dt>
90 <dt><span class="section"><a href="atomic/porting.html#atomic.porting.tested_compilers">Tested compilers</a></span></dt>
91 <dt><span class="section"><a href="atomic/porting.html#atomic.porting.acknowledgements">Acknowledgements</a></span></dt>
92 </dl></dd>
93 </dl>
94 </div>
95 <div class="section">
96 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
97 <a name="atomic.introduction"></a><a class="link" href="atomic.html#atomic.introduction" title="Introduction">Introduction</a>
98 </h2></div></div></div>
99 <div class="toc"><dl class="toc">
100 <dt><span class="section"><a href="atomic.html#atomic.introduction.introduction_presenting">Presenting
101       Boost.Atomic</a></span></dt>
102 <dt><span class="section"><a href="atomic.html#atomic.introduction.introduction_purpose">Purpose</a></span></dt>
103 </dl></div>
104 <div class="section">
105 <div class="titlepage"><div><div><h3 class="title">
106 <a name="atomic.introduction.introduction_presenting"></a><a class="link" href="atomic.html#atomic.introduction.introduction_presenting" title="Presenting Boost.Atomic">Presenting
107       Boost.Atomic</a>
108 </h3></div></div></div>
109 <p>
110         <span class="bold"><strong>Boost.Atomic</strong></span> is a library that provides
111         <code class="literal">atomic</code> data types and operations on these data types,
112         as well as memory ordering constraints required for coordinating multiple
113         threads through atomic variables. It implements the interface as defined
114         by the C++11 standard, but makes this feature available for platforms lacking
115         system/compiler support for this particular C++11 feature.
116       </p>
117 <p>
118         Users of this library should already be familiar with concurrency in general,
119         as well as elementary concepts such as "mutual exclusion".
120       </p>
121 <p>
122         The implementation makes use of processor-specific instructions where possible
123         (via inline assembler, platform libraries or compiler intrinsics), and falls
124         back to "emulating" atomic operations through locking.
125       </p>
126 </div>
127 <div class="section">
128 <div class="titlepage"><div><div><h3 class="title">
129 <a name="atomic.introduction.introduction_purpose"></a><a class="link" href="atomic.html#atomic.introduction.introduction_purpose" title="Purpose">Purpose</a>
130 </h3></div></div></div>
131 <p>
132         Operations on "ordinary" variables are not guaranteed to be atomic.
133         This means that with <code class="literal">int n=0</code> initially, two threads concurrently
134         executing
135       </p>
136 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">function</span><span class="special">()</span>
137 <span class="special">{</span>
138   <span class="identifier">n</span> <span class="special">++;</span>
139 <span class="special">}</span>
140 </pre>
141 <p>
142         might result in <code class="literal">n==1</code> instead of 2: Each thread will read
143         the old value into a processor register, increment it and write the result
144         back. Both threads may therefore write <code class="literal">1</code>, unaware that
145         the other thread is doing likewise.
146       </p>
147 <p>
148         Declaring <code class="literal">atomic&lt;int&gt; n=0</code> instead, the same operation
149         on this variable will always result in <code class="literal">n==2</code> as each operation
150         on this variable is <span class="emphasis"><em>atomic</em></span>: This means that each operation
151         behaves as if it were strictly sequentialized with respect to the other.
152       </p>
153 <p>
154         Atomic variables are useful for two purposes:
155       </p>
156 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
157 <li class="listitem">
158             as a means for coordinating multiple threads via custom coordination
159             protocols
160           </li>
161 <li class="listitem">
162             as faster alternatives to "locked" access to simple variables
163           </li>
164 </ul></div>
165 <p>
166         Take a look at the <a class="link" href="atomic/usage_examples.html" title="Usage examples">examples</a>
167         section for common patterns.
168       </p>
169 </div>
170 </div>
171 </div>
172 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
173 <td align="left"><p><small>Last revised: April 17, 2017 at 02:30:28 GMT</small></p></td>
174 <td align="right"><div class="copyright-footer"></div></td>
175 </tr></table>
176 <hr>
177 <div class="spirit-nav">
178 <a accesskey="p" href="array/ack.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.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="atomic/thread_coordination.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
179 </div>
180 </body>
181 </html>