3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Coroutine</title>
5 <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../index.html" title="Chapter 1. Coroutine">
8 <link rel="up" href="../index.html" title="Chapter 1. Coroutine">
9 <link rel="prev" href="motivation.html" title="Motivation">
10 <link rel="next" href="coroutine/asymmetric.html" title="Asymmetric coroutine">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="motivation.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="coroutine/asymmetric.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="coroutine.coroutine"></a><a class="link" href="coroutine.html" title="Coroutine">Coroutine</a>
28 </h2></div></div></div>
29 <div class="toc"><dl class="toc">
30 <dt><span class="section"><a href="coroutine/asymmetric.html">Asymmetric coroutine</a></span></dt>
32 <dt><span class="section"><a href="coroutine/asymmetric/pull_coro.html">Class <code class="computeroutput"><span class="identifier">asymmetric_coroutine</span><span class="special"><>::</span><span class="identifier">pull_type</span></code></a></span></dt>
33 <dt><span class="section"><a href="coroutine/asymmetric/push_coro.html">Class <code class="computeroutput"><span class="identifier">asymmetric_coroutine</span><span class="special"><>::</span><span class="identifier">push_type</span></code></a></span></dt>
35 <dt><span class="section"><a href="coroutine/symmetric.html">Symmetric coroutine</a></span></dt>
37 <dt><span class="section"><a href="coroutine/symmetric/symmetric_coro.html">Class
38 <code class="computeroutput"><span class="identifier">symmetric_coroutine</span><span class="special"><>::</span><span class="identifier">call_type</span></code></a></span></dt>
39 <dt><span class="section"><a href="coroutine/symmetric/yield_coro.html">Class <code class="computeroutput"><span class="identifier">symmetric_coroutine</span><span class="special"><>::</span><span class="identifier">yield_type</span></code></a></span></dt>
43 <span class="bold"><strong>Boost.Coroutine</strong></span> provides two implementations
44 - asymmetric and symmetric coroutines.
47 Symmetric coroutines occur usually in the context of concurrent programming
48 in order to represent independent units of execution. Implementations that
49 produce sequences of values typically use asymmetric coroutines. <a href="#ftn.coroutine.coroutine.f0" class="footnote" name="coroutine.coroutine.f0"><sup class="footnote">[5]</sup></a>
52 <a name="coroutine.coroutine.h0"></a>
53 <span class="phrase"><a name="coroutine.coroutine.stackful"></a></span><a class="link" href="coroutine.html#coroutine.coroutine.stackful">stackful</a>
56 Each instance of a coroutine has its own stack.
59 In contrast to stackless coroutines, stackful coroutines allow invoking the
60 suspend operation out of arbitrary sub-stackframes, enabling escape-and-reenter
64 <a name="coroutine.coroutine.h1"></a>
65 <span class="phrase"><a name="coroutine.coroutine.move_only"></a></span><a class="link" href="coroutine.html#coroutine.coroutine.move_only">move-only</a>
68 A coroutine is moveable-only.
71 If it were copyable, then its stack with all the objects allocated on it would
72 be copied too. That would force undefined behaviour if some of these objects
73 were RAII-classes (manage a resource via RAII pattern). When the first of the
74 coroutine copies terminates (unwinds its stack), the RAII class destructors
75 will release their managed resources. When the second copy terminates, the
76 same destructors will try to doubly-release the same resources, leading to
80 <a name="coroutine.coroutine.h2"></a>
81 <span class="phrase"><a name="coroutine.coroutine.clean_up"></a></span><a class="link" href="coroutine.html#coroutine.coroutine.clean_up">clean-up</a>
84 On coroutine destruction the associated stack will be unwound.
87 The constructor of coroutine allows you to pass a customized <span class="emphasis"><em>stack-allocator</em></span>.
88 <span class="emphasis"><em>stack-allocator</em></span> is free to deallocate the stack or cache
89 it for future usage (for coroutines created later).
92 <a name="coroutine.coroutine.h3"></a>
93 <span class="phrase"><a name="coroutine.coroutine.segmented_stack"></a></span><a class="link" href="coroutine.html#coroutine.coroutine.segmented_stack">segmented
97 <span class="emphasis"><em>symmetric_coroutine<>::call_type</em></span>, <span class="emphasis"><em>asymmetric_coroutine<>::push_type</em></span>
98 and <span class="emphasis"><em>asymmetric_coroutine<>::pull_type</em></span> support segmented
99 stacks (growing on demand).
102 It is not always possible to accurately estimate the required stack size -
103 in most cases too much memory is allocated (waste of virtual address-space).
106 At construction a coroutine starts with a default (minimal) stack size. This
107 minimal stack size is the maximum of page size and the canonical size for signal
108 stack (macro SIGSTKSZ on POSIX).
111 At this time of writing only GCC (4.7) <a href="#ftn.coroutine.coroutine.f1" class="footnote" name="coroutine.coroutine.f1"><sup class="footnote">[6]</sup></a> is known to support segmented stacks. With version 1.54 <span class="bold"><strong>Boost.Coroutine</strong></span> provides support for segmented stacks.
114 The destructor releases the associated stack. The implementer is free to deallocate
115 the stack or to cache it for later usage.
118 <a name="coroutine.coroutine.h4"></a>
119 <span class="phrase"><a name="coroutine.coroutine.context_switch"></a></span><a class="link" href="coroutine.html#coroutine.coroutine.context_switch">context
123 A coroutine saves and restores registers according to the underlying ABI on
124 each context switch (using <span class="bold"><strong>Boost.Context</strong></span>).
127 Some applications do not use floating-point registers and can disable preserving
128 FPU registers for performance reasons.
130 <div class="note"><table border="0" summary="Note">
132 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
133 <th align="left">Note</th>
135 <tr><td align="left" valign="top"><p>
136 According to the calling convention the FPU registers are preserved by default.
140 On POSIX systems, the coroutine context switch does not preserve signal masks
141 for performance reasons.
144 A context switch is done via <span class="emphasis"><em>symmetric_coroutine<>::call_type::operator()</em></span>,
145 <span class="emphasis"><em>asymmetric_coroutine<>::push_type::operator()</em></span> and
146 <span class="emphasis"><em>asymmetric_coroutine<>::pull_type::operator()</em></span>.
148 <div class="warning"><table border="0" summary="Warning">
150 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../doc/src/images/warning.png"></td>
151 <th align="left">Warning</th>
153 <tr><td align="left" valign="top"><p>
154 Calling <span class="emphasis"><em>symmetric_coroutine<>::call_type::operator()</em></span>,
155 <span class="emphasis"><em>asymmetric_coroutine<>::push_type::operator()</em></span>
156 and <span class="emphasis"><em>asymmetric_coroutine<>::pull_type::operator()</em></span>
157 from inside the <span class="underline">same</span> coroutine results
158 in undefined behaviour.
162 As an example, the code below will result in undefined behaviour:
164 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">coroutines</span><span class="special">::</span><span class="identifier">symmetric_coroutine</span><span class="special"><</span><span class="keyword">void</span><span class="special">>::</span><span class="identifier">call_type</span> <span class="identifier">coro</span><span class="special">(</span>
165 <span class="special">[&](</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">coroutines</span><span class="special">::</span><span class="identifier">symmetric_coroutine</span><span class="special"><</span><span class="keyword">void</span><span class="special">>::</span><span class="identifier">yield_type</span><span class="special">&</span> <span class="identifier">yield</span><span class="special">){</span>
166 <span class="identifier">yield</span><span class="special">(</span><span class="identifier">coro</span><span class="special">);</span> <span class="comment">// yield to same symmetric_coroutine</span>
167 <span class="special">});</span>
168 <span class="identifier">coro</span><span class="special">();</span>
170 <div class="footnotes">
171 <br><hr style="width:100; text-align:left;margin-left: 0">
172 <div id="ftn.coroutine.coroutine.f0" class="footnote"><p><a href="#coroutine.coroutine.f0" class="para"><sup class="para">[5] </sup></a>
173 Moura, Ana Lucia De and Ierusalimschy, Roberto. "Revisiting coroutines".
174 ACM Trans. Program. Lang. Syst., Volume 31 Issue 2, February 2009, Article
177 <div id="ftn.coroutine.coroutine.f1" class="footnote"><p><a href="#coroutine.coroutine.f1" class="para"><sup class="para">[6] </sup></a>
178 <a href="http://gcc.gnu.org/wiki/SplitStacks" target="_top">Ian Lance Taylor, Split
183 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
184 <td align="left"></td>
185 <td align="right"><div class="copyright-footer">Copyright © 2009 Oliver Kowalke<p>
186 Distributed under the Boost Software License, Version 1.0. (See accompanying
187 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>)
192 <div class="spirit-nav">
193 <a accesskey="p" href="motivation.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="coroutine/asymmetric.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>