1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>spawn</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="../reference.html" title="Reference"> 9<link rel="prev" href="socket_base/_socket_base.html" title="socket_base::~socket_base"> 10<link rel="next" href="spawn/overload1.html" title="spawn (1 of 7 overloads)"> 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="socket_base/_socket_base.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="spawn/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_asio.reference.spawn"></a><a class="link" href="spawn.html" title="spawn">spawn</a> 28</h3></div></div></div> 29<p> 30 <a class="indexterm" name="boost_asio.indexterm.spawn"></a> 31Start a new stackful coroutine. 32 </p> 33<p> 34 Start a new stackful coroutine, calling the specified handler when it completes. 35 </p> 36<pre class="programlisting">template< 37 typename Function> 38void <a class="link" href="spawn/overload1.html" title="spawn (1 of 7 overloads)">spawn</a>( 39 Function && function, 40 const boost::coroutines::attributes & attributes = boost::coroutines::attributes()); 41 <span class="emphasis"><em>» <a class="link" href="spawn/overload1.html" title="spawn (1 of 7 overloads)">more...</a></em></span> 42 43template< 44 typename <a class="link" href="Handler.html" title="Handlers">Handler</a>, 45 typename Function> 46void <a class="link" href="spawn/overload2.html" title="spawn (2 of 7 overloads)">spawn</a>( 47 Handler && handler, 48 Function && function, 49 const boost::coroutines::attributes & attributes = boost::coroutines::attributes(), 50 typename enable_if< !is_executor< typename decay< Handler >::type >::value &&!execution::is_executor< typename decay< Handler >::type >::value &&!is_convertible< Handler &, execution_context & >::value >::type * = 0); 51 <span class="emphasis"><em>» <a class="link" href="spawn/overload2.html" title="spawn (2 of 7 overloads)">more...</a></em></span> 52</pre> 53<p> 54 Start a new stackful coroutine, inheriting the execution context of another. 55 </p> 56<pre class="programlisting">template< 57 typename <a class="link" href="Handler.html" title="Handlers">Handler</a>, 58 typename Function> 59void <a class="link" href="spawn/overload3.html" title="spawn (3 of 7 overloads)">spawn</a>( 60 basic_yield_context< Handler > ctx, 61 Function && function, 62 const boost::coroutines::attributes & attributes = boost::coroutines::attributes()); 63 <span class="emphasis"><em>» <a class="link" href="spawn/overload3.html" title="spawn (3 of 7 overloads)">more...</a></em></span> 64</pre> 65<p> 66 Start a new stackful coroutine that executes on a given executor. 67 </p> 68<pre class="programlisting">template< 69 typename Function, 70 typename <a class="link" href="Executor1.html" title="Executor requirements">Executor</a>> 71void <a class="link" href="spawn/overload4.html" title="spawn (4 of 7 overloads)">spawn</a>( 72 const Executor & ex, 73 Function && function, 74 const boost::coroutines::attributes & attributes = boost::coroutines::attributes(), 75 typename enable_if< is_executor< Executor >::value||execution::is_executor< Executor >::value >::type * = 0); 76 <span class="emphasis"><em>» <a class="link" href="spawn/overload4.html" title="spawn (4 of 7 overloads)">more...</a></em></span> 77</pre> 78<p> 79 Start a new stackful coroutine that executes on a given strand. 80 </p> 81<pre class="programlisting">template< 82 typename Function, 83 typename <a class="link" href="Executor1.html" title="Executor requirements">Executor</a>> 84void <a class="link" href="spawn/overload5.html" title="spawn (5 of 7 overloads)">spawn</a>( 85 const strand< Executor > & ex, 86 Function && function, 87 const boost::coroutines::attributes & attributes = boost::coroutines::attributes()); 88 <span class="emphasis"><em>» <a class="link" href="spawn/overload5.html" title="spawn (5 of 7 overloads)">more...</a></em></span> 89</pre> 90<p> 91 Start a new stackful coroutine that executes in the context of a strand. 92 </p> 93<pre class="programlisting">template< 94 typename Function> 95void <a class="link" href="spawn/overload6.html" title="spawn (6 of 7 overloads)">spawn</a>( 96 const boost::asio::io_context::strand & s, 97 Function && function, 98 const boost::coroutines::attributes & attributes = boost::coroutines::attributes()); 99 <span class="emphasis"><em>» <a class="link" href="spawn/overload6.html" title="spawn (6 of 7 overloads)">more...</a></em></span> 100</pre> 101<p> 102 Start a new stackful coroutine that executes on a given execution context. 103 </p> 104<pre class="programlisting">template< 105 typename Function, 106 typename ExecutionContext> 107void <a class="link" href="spawn/overload7.html" title="spawn (7 of 7 overloads)">spawn</a>( 108 ExecutionContext & ctx, 109 Function && function, 110 const boost::coroutines::attributes & attributes = boost::coroutines::attributes(), 111 typename enable_if< is_convertible< ExecutionContext &, execution_context & >::value >::type * = 0); 112 <span class="emphasis"><em>» <a class="link" href="spawn/overload7.html" title="spawn (7 of 7 overloads)">more...</a></em></span> 113</pre> 114<p> 115 The <code class="computeroutput">spawn()</code> function is a high-level wrapper over the Boost.Coroutine 116 library. This function enables programs to implement asynchronous logic in 117 a synchronous manner, as illustrated by the following example: 118 </p> 119<pre class="programlisting">boost::asio::spawn(my_strand, do_echo); 120 121// ... 122 123void do_echo(boost::asio::yield_context yield) 124{ 125 try 126 { 127 char data[128]; 128 for (;;) 129 { 130 std::size_t length = 131 my_socket.async_read_some( 132 boost::asio::buffer(data), yield); 133 134 boost::asio::async_write(my_socket, 135 boost::asio::buffer(data, length), yield); 136 } 137 } 138 catch (std::exception& e) 139 { 140 // ... 141 } 142} 143</pre> 144<h5> 145<a name="boost_asio.reference.spawn.h0"></a> 146 <span class="phrase"><a name="boost_asio.reference.spawn.requirements"></a></span><a class="link" href="spawn.html#boost_asio.reference.spawn.requirements">Requirements</a> 147 </h5> 148<p> 149 <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/spawn.hpp</code> 150 </p> 151<p> 152 <span class="emphasis"><em>Convenience header: </em></span>None 153 </p> 154</div> 155<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 156<td align="left"></td> 157<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 158 Kohlhoff<p> 159 Distributed under the Boost Software License, Version 1.0. (See accompanying 160 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>) 161 </p> 162</div></td> 163</tr></table> 164<hr> 165<div class="spirit-nav"> 166<a accesskey="p" href="socket_base/_socket_base.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="spawn/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 167</div> 168</body> 169</html> 170