1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Stackful Coroutines</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="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../core.html" title="Core Concepts and Functionality"> 9<link rel="prev" href="coroutine.html" title="Stackless Coroutines"> 10<link rel="next" href="coroutines_ts.html" title="Coroutines TS Support"> 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="coroutine.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="coroutines_ts.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.overview.core.spawn"></a><a class="link" href="spawn.html" title="Stackful Coroutines">Stackful Coroutines</a> 28</h4></div></div></div> 29<p> 30 The <a class="link" href="../../reference/spawn.html" title="spawn"><code class="computeroutput">spawn()</code></a> 31 function is a high-level wrapper for running stackful coroutines. It is 32 based on the Boost.Coroutine library. The <code class="computeroutput">spawn()</code> function 33 enables programs to implement asynchronous logic in a synchronous manner, 34 as shown in the following example: 35 </p> 36<pre class="programlisting">boost::asio::spawn(my_strand, do_echo); 37 38// ... 39 40void do_echo(boost::asio::yield_context yield) 41{ 42 try 43 { 44 char data[128]; 45 for (;;) 46 { 47 std::size_t length = 48 my_socket.async_read_some( 49 boost::asio::buffer(data), yield); 50 51 boost::asio::async_write(my_socket, 52 boost::asio::buffer(data, length), yield); 53 } 54 } 55 catch (std::exception& e) 56 { 57 // ... 58 } 59} 60</pre> 61<p> 62 The first argument to <code class="computeroutput">spawn()</code> may be a <a class="link" href="../../reference/io_context__strand.html" title="io_context::strand"><code class="computeroutput">strand</code></a>, 63 <a class="link" href="../../reference/io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>, 64 or <a class="link" href="../../reference/CompletionHandler.html" title="Completion handler requirements">completion handler</a>. 65 This argument determines the context in which the coroutine is permitted 66 to execute. For example, a server's per-client object may consist of multiple 67 coroutines; they should all run on the same <code class="computeroutput">strand</code> so that 68 no explicit synchronisation is required. 69 </p> 70<p> 71 The second argument is a function object with signature: 72 </p> 73<pre class="programlisting">void coroutine(boost::asio::yield_context yield); 74</pre> 75<p> 76 that specifies the code to be run as part of the coroutine. The parameter 77 <code class="computeroutput">yield</code> may be passed to an asynchronous operation in place 78 of the completion handler, as in: 79 </p> 80<pre class="programlisting">std::size_t length = 81 my_socket.async_read_some( 82 boost::asio::buffer(data), yield); 83</pre> 84<p> 85 This starts the asynchronous operation and suspends the coroutine. The 86 coroutine will be resumed automatically when the asynchronous operation 87 completes. 88 </p> 89<p> 90 Where an asynchronous operation's handler signature has the form: 91 </p> 92<pre class="programlisting">void handler(boost::system::error_code ec, result_type result); 93</pre> 94<p> 95 the initiating function returns the result_type. In the <code class="computeroutput">async_read_some</code> 96 example above, this is <code class="computeroutput">size_t</code>. If the asynchronous operation 97 fails, the <code class="computeroutput">error_code</code> is converted into a <code class="computeroutput">system_error</code> 98 exception and thrown. 99 </p> 100<p> 101 Where a handler signature has the form: 102 </p> 103<pre class="programlisting">void handler(boost::system::error_code ec); 104</pre> 105<p> 106 the initiating function returns <code class="computeroutput">void</code>. As above, an error is 107 passed back to the coroutine as a <code class="computeroutput">system_error</code> exception. 108 </p> 109<p> 110 To collect the <code class="computeroutput">error_code</code> from an operation, rather than have 111 it throw an exception, associate the output variable with the <code class="computeroutput">yield_context</code> 112 as follows: 113 </p> 114<pre class="programlisting">boost::system::error_code ec; 115std::size_t length = 116 my_socket.async_read_some( 117 boost::asio::buffer(data), yield[ec]); 118</pre> 119<p> 120 <span class="bold"><strong>Note:</strong></span> if <code class="computeroutput">spawn()</code> is used 121 with a custom completion handler of type <code class="computeroutput">Handler</code>, the function 122 object signature is actually: 123 </p> 124<pre class="programlisting">void coroutine(boost::asio::basic_yield_context<Handler> yield); 125</pre> 126<h6> 127<a name="boost_asio.overview.core.spawn.h0"></a> 128 <span class="phrase"><a name="boost_asio.overview.core.spawn.see_also"></a></span><a class="link" href="spawn.html#boost_asio.overview.core.spawn.see_also">See 129 Also</a> 130 </h6> 131<p> 132 <a class="link" href="../../reference/spawn.html" title="spawn">spawn</a>, <a class="link" href="../../reference/yield_context.html" title="yield_context">yield_context</a>, 133 <a class="link" href="../../reference/basic_yield_context.html" title="basic_yield_context">basic_yield_context</a>, 134 <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.spawn">Spawn example 135 (C++03)</a>, <a class="link" href="../../examples/cpp11_examples.html#boost_asio.examples.cpp11_examples.spawn">Spawn 136 example (C++11)</a>, <a class="link" href="coroutine.html" title="Stackless Coroutines">Stackless 137 Coroutines</a>. 138 </p> 139</div> 140<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 141<td align="left"></td> 142<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 143 Kohlhoff<p> 144 Distributed under the Boost Software License, Version 1.0. (See accompanying 145 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>) 146 </p> 147</div></td> 148</tr></table> 149<hr> 150<div class="spirit-nav"> 151<a accesskey="p" href="coroutine.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="coroutines_ts.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 152</div> 153</body> 154</html> 155