• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Rationale</title>
5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Chapter 1. Fiber">
8<link rel="up" href="../index.html" title="Chapter 1. Fiber">
9<link rel="prev" href="custom.html" title="Customization">
10<link rel="next" href="acknowledgements.html" title="Acknowledgments">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="custom.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="fiber.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a>
28</h2></div></div></div>
29<h4>
30<a name="fiber.rationale.h0"></a>
31      <span class="phrase"><a name="fiber.rationale.preprocessor_defines"></a></span><a class="link" href="rationale.html#fiber.rationale.preprocessor_defines">preprocessor
32      defines</a>
33    </h4>
34<div class="table">
35<a name="fiber.rationale.preopcessor_defines"></a><p class="title"><b>Table 1.6. preopcessor defines</b></p>
36<div class="table-contents"><table class="table" summary="preopcessor defines">
37<colgroup>
38<col>
39<col>
40</colgroup>
41<thead><tr>
42<th>
43            </th>
44<th>
45            </th>
46</tr></thead>
47<tbody>
48<tr>
49<td>
50              <p>
51                BOOST_FIBERS_NO_ATOMICS
52              </p>
53            </td>
54<td>
55              <p>
56                no <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;&gt;</span></code>
57                used, inter-thread synchronization disabled
58              </p>
59            </td>
60</tr>
61<tr>
62<td>
63              <p>
64                BOOST_FIBERS_SPINLOCK_STD_MUTEX
65              </p>
66            </td>
67<td>
68              <p>
69                use <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span></code> as spinlock instead of default
70                <code class="computeroutput"><span class="identifier">XCHG</span></code>-sinlock with
71                backoff
72              </p>
73            </td>
74</tr>
75<tr>
76<td>
77              <p>
78                BOOST_FIBERS_SPIN_BACKOFF
79              </p>
80            </td>
81<td>
82              <p>
83                limit determines when to used <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code> instead of mnemonic <code class="computeroutput"><span class="identifier">pause</span><span class="special">/</span><span class="identifier">yield</span></code> during busy wait (apllies
84                on to <code class="computeroutput"><span class="identifier">XCHG</span></code>-spinlock)
85              </p>
86            </td>
87</tr>
88<tr>
89<td>
90              <p>
91                BOOST_FIBERS_SINGLE_CORE
92              </p>
93            </td>
94<td>
95              <p>
96                allways call <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code> without backoff during busy wait
97                (apllies on to <code class="computeroutput"><span class="identifier">XCHG</span></code>-spinlock)
98              </p>
99            </td>
100</tr>
101</tbody>
102</table></div>
103</div>
104<br class="table-break"><h4>
105<a name="fiber.rationale.h1"></a>
106      <span class="phrase"><a name="fiber.rationale.distinction_between_coroutines_and_fibers"></a></span><a class="link" href="rationale.html#fiber.rationale.distinction_between_coroutines_and_fibers">distinction
107      between coroutines and fibers</a>
108    </h4>
109<p>
110      The fiber library extends the coroutine library by adding a scheduler and synchronization
111      mechanisms.
112    </p>
113<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
114<li class="listitem">
115          a coroutine yields
116        </li>
117<li class="listitem">
118          a fiber blocks
119        </li>
120</ul></div>
121<p>
122      When a coroutine yields, it passes control directly to its caller (or, in the
123      case of symmetric coroutines, a designated other coroutine). When a fiber blocks,
124      it implicitly passes control to the fiber scheduler. Coroutines have no scheduler
125      because they need no scheduler.<a href="#ftn.fiber.rationale.f0" class="footnote" name="fiber.rationale.f0"><sup class="footnote">[12]</sup></a>.
126    </p>
127<h4>
128<a name="fiber.rationale.h2"></a>
129      <span class="phrase"><a name="fiber.rationale.what_about_transactional_memory"></a></span><a class="link" href="rationale.html#fiber.rationale.what_about_transactional_memory">what
130      about transactional memory</a>
131    </h4>
132<p>
133      GCC supports transactional memory since version 4.7. Unfortunately tests show
134      that transactional memory is slower (ca. 4x) than spinlocks using atomics.
135      Once transactional memory is improved (supporting hybrid tm), spinlocks will
136      be replaced by __transaction_atomic{} statements surrounding the critical sections.
137    </p>
138<h4>
139<a name="fiber.rationale.h3"></a>
140      <span class="phrase"><a name="fiber.rationale.synchronization_between_fibers_running_in_different_threads"></a></span><a class="link" href="rationale.html#fiber.rationale.synchronization_between_fibers_running_in_different_threads">synchronization
141      between fibers running in different threads</a>
142    </h4>
143<p>
144      Synchronization classes from <a href="http://www.boost.org/doc/libs/release/libs/thread/index.html" target="_top">Boost.Thread</a>
145      block the entire thread. In contrast, the synchronization classes from <span class="bold"><strong>Boost.Fiber</strong></span> block only specific fibers, so that the
146      scheduler can still keep the thread busy running other fibers in the meantime.
147      The synchronization classes from <span class="bold"><strong>Boost.Fiber</strong></span>
148      are designed to be thread-safe, i.e. it is possible to synchronize fibers running
149      in different threads as well as fibers running in the same thread. (However,
150      there is a build option to disable cross-thread fiber synchronization support;
151      see <a class="link" href="overview.html#cross_thread_sync">this description</a>.)
152    </p>
153<a name="spurious_wakeup"></a><h4>
154<a name="fiber.rationale.h4"></a>
155      <span class="phrase"><a name="fiber.rationale.spurious_wakeup"></a></span><a class="link" href="rationale.html#fiber.rationale.spurious_wakeup">spurious
156      wakeup</a>
157    </h4>
158<p>
159      Spurious wakeup can happen when using <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code></a>:
160      the condition variable appears to be have been signaled while the awaited condition
161      may still be false. Spurious wakeup can happen repeatedly and is caused on
162      some multiprocessor systems where making <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code>
163      wakeup completely predictable would slow down all <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code>
164      operations.<a href="#ftn.fiber.rationale.f1" class="footnote" name="fiber.rationale.f1"><sup class="footnote">[13]</sup></a>
165    </p>
166<p>
167      <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> is not subject to spurious wakeup.
168      Nonetheless it is prudent to test the business-logic condition in a <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> loop
169      — or, equivalently, use one of the <code class="computeroutput"><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lock</span><span class="special">,</span>
170      <span class="identifier">predicate</span> <span class="special">)</span></code>
171      overloads.
172    </p>
173<p>
174      See also <a class="link" href="synchronization/conditions.html#condition_variable_spurious_wakeups">No Spurious Wakeups</a>.
175    </p>
176<h4>
177<a name="fiber.rationale.h5"></a>
178      <span class="phrase"><a name="fiber.rationale.migrating_fibers_between_threads"></a></span><a class="link" href="rationale.html#fiber.rationale.migrating_fibers_between_threads">migrating
179      fibers between threads</a>
180    </h4>
181<p>
182      Support for migrating fibers between threads has been integrated. The user-defined
183      scheduler must call <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a> on a fiber-context on the
184      source thread and <a class="link" href="scheduling.html#context_attach"><code class="computeroutput">context::attach()</code></a> on the destination thread,
185      passing the fiber-context to migrate. (For more information about custom schedulers,
186      see <a class="link" href="custom.html#custom">Customization</a>.) Examples <code class="computeroutput"><span class="identifier">work_sharing</span></code>
187      and <code class="computeroutput"><span class="identifier">work_stealing</span></code> in directory
188      <code class="computeroutput"><span class="identifier">examples</span></code> might be used as a
189      blueprint.
190    </p>
191<p>
192      See also <a class="link" href="migration.html#migration">Migrating fibers between threads</a>.
193    </p>
194<h4>
195<a name="fiber.rationale.h6"></a>
196      <span class="phrase"><a name="fiber.rationale.support_for_boost_asio"></a></span><a class="link" href="rationale.html#fiber.rationale.support_for_boost_asio">support
197      for Boost.Asio</a>
198    </h4>
199<p>
200      Support for <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a>’s
201      <span class="emphasis"><em>async-result</em></span> is not part of the official API. However,
202      to integrate with a <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span></code></a>,
203      see <a class="link" href="integration.html#integration">Sharing a Thread with Another Main Loop</a>.
204      To interface smoothly with an arbitrary Asio async I/O operation, see <a class="link" href="callbacks/then_there_s____boost_asio__.html#callbacks_asio">Then There’s Boost.Asio</a>.
205    </p>
206<h4>
207<a name="fiber.rationale.h7"></a>
208      <span class="phrase"><a name="fiber.rationale.tested_compilers"></a></span><a class="link" href="rationale.html#fiber.rationale.tested_compilers">tested
209      compilers</a>
210    </h4>
211<p>
212      The library was tested with GCC-5.1.1, Clang-3.6.0 and MSVC-14.0 in c++11-mode.
213    </p>
214<h4>
215<a name="fiber.rationale.h8"></a>
216      <span class="phrase"><a name="fiber.rationale.supported_architectures"></a></span><a class="link" href="rationale.html#fiber.rationale.supported_architectures">supported
217      architectures</a>
218    </h4>
219<p>
220      <span class="bold"><strong>Boost.Fiber</strong></span> depends on <a href="http://www.boost.org/doc/libs/release/libs/context/index.html" target="_top">Boost.Context</a>
221      - the list of supported architectures can be found <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/architectures.html" target="_top">here</a>.
222    </p>
223<div class="footnotes">
224<br><hr style="width:100; text-align:left;margin-left: 0">
225<div id="ftn.fiber.rationale.f0" class="footnote"><p><a href="#fiber.rationale.f0" class="para"><sup class="para">[12] </sup></a>
226        <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4024.pdf" target="_top">'N4024:
227        Distinguishing coroutines and fibers'</a>
228      </p></div>
229<div id="ftn.fiber.rationale.f1" class="footnote"><p><a href="#fiber.rationale.f1" class="para"><sup class="para">[13] </sup></a>
230        David R. Butenhof <span class="quote">“<span class="quote">Programming with POSIX Threads</span>”</span>
231      </p></div>
232</div>
233</div>
234<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
235<td align="left"></td>
236<td align="right"><div class="copyright-footer">Copyright © 2013 Oliver Kowalke<p>
237        Distributed under the Boost Software License, Version 1.0. (See accompanying
238        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>)
239      </p>
240</div></td>
241</tr></table>
242<hr>
243<div class="spirit-nav">
244<a accesskey="p" href="custom.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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>
245</div>
246</body>
247</html>
248