• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>co_spawn (4 of 6 overloads)</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="../co_spawn.html" title="co_spawn">
9<link rel="prev" href="overload3.html" title="co_spawn (3 of 6 overloads)">
10<link rel="next" href="overload5.html" title="co_spawn (5 of 6 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="overload3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../co_spawn.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="overload5.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.reference.co_spawn.overload4"></a><a class="link" href="overload4.html" title="co_spawn (4 of 6 overloads)">co_spawn (4
28        of 6 overloads)</a>
29</h4></div></div></div>
30<p>
31          Spawn a new coroutined-based thread of execution.
32        </p>
33<pre class="programlisting">template&lt;
34    typename ExecutionContext,
35    typename <a class="link" href="../Executor1.html" title="Executor requirements">AwaitableExecutor</a>,
36    typename CompletionToken = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>&gt;
37<a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.automatic_deduction_of_initiating_function_return_type"><span class="emphasis"><em>DEDUCED</em></span></a> co_spawn(
38    ExecutionContext &amp; ctx,
39    awaitable&lt; void, AwaitableExecutor &gt; a,
40    CompletionToken &amp;&amp; token = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>,
41    typename enable_if&lt; is_convertible&lt; ExecutionContext &amp;, execution_context &amp; &gt;::value &amp;&amp;is_convertible&lt; typename ExecutionContext::executor_type, AwaitableExecutor &gt;::value &gt;::type *  = 0);
42</pre>
43<h6>
44<a name="boost_asio.reference.co_spawn.overload4.h0"></a>
45          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload4.parameters"></a></span><a class="link" href="overload4.html#boost_asio.reference.co_spawn.overload4.parameters">Parameters</a>
46        </h6>
47<div class="variablelist">
48<p class="title"><b></b></p>
49<dl class="variablelist">
50<dt><span class="term">ctx</span></dt>
51<dd><p>
52                An execution context that will provide the executor to be used to
53                schedule the new thread of execution.
54              </p></dd>
55<dt><span class="term">a</span></dt>
56<dd><p>
57                The <a class="link" href="../awaitable.html" title="awaitable"><code class="computeroutput">awaitable</code></a>
58                object that is the result of calling the coroutine's entry point
59                function.
60              </p></dd>
61<dt><span class="term">token</span></dt>
62<dd>
63<p>
64                The completion token that will handle the notification that the thread
65                of execution has completed. The function signature of the completion
66                handler must be:
67</p>
68<pre class="programlisting">void handler(std::exception_ptr);
69</pre>
70<p>
71              </p>
72</dd>
73</dl>
74</div>
75<h6>
76<a name="boost_asio.reference.co_spawn.overload4.h1"></a>
77          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload4.example"></a></span><a class="link" href="overload4.html#boost_asio.reference.co_spawn.overload4.example">Example</a>
78        </h6>
79<pre class="programlisting">boost::asio::awaitable&lt;void&gt; echo(tcp::socket socket)
80{
81  try
82  {
83    char data[1024];
84    for (;;)
85    {
86      std::size_t n = co_await socket.async_read_some(
87          boost::asio::buffer(data), boost::asio::use_awaitable);
88
89      co_await boost::asio::async_write(socket,
90          boost::asio::buffer(data, n), boost::asio::use_awaitable);
91    }
92  }
93  catch (const std::exception&amp; e)
94  {
95    std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
96  }
97}
98
99// ...
100
101boost::asio::co_spawn(my_io_context,
102  echo(std::move(my_tcp_socket)),
103  boost::asio::detached);
104</pre>
105</div>
106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
107<td align="left"></td>
108<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
109      Kohlhoff<p>
110        Distributed under the Boost Software License, Version 1.0. (See accompanying
111        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>)
112      </p>
113</div></td>
114</tr></table>
115<hr>
116<div class="spirit-nav">
117<a accesskey="p" href="overload3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../co_spawn.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="overload5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
118</div>
119</body>
120</html>
121