• 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 (3 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="overload2.html" title="co_spawn (2 of 6 overloads)">
10<link rel="next" href="overload4.html" title="co_spawn (4 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="overload2.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="overload4.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.overload3"></a><a class="link" href="overload3.html" title="co_spawn (3 of 6 overloads)">co_spawn (3
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 T,
36    typename <a class="link" href="../Executor1.html" title="Executor requirements">AwaitableExecutor</a>,
37    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;
38<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(
39    ExecutionContext &amp; ctx,
40    awaitable&lt; T, AwaitableExecutor &gt; a,
41    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>,
42    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);
43</pre>
44<h6>
45<a name="boost_asio.reference.co_spawn.overload3.h0"></a>
46          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload3.parameters"></a></span><a class="link" href="overload3.html#boost_asio.reference.co_spawn.overload3.parameters">Parameters</a>
47        </h6>
48<div class="variablelist">
49<p class="title"><b></b></p>
50<dl class="variablelist">
51<dt><span class="term">ctx</span></dt>
52<dd><p>
53                An execution context that will provide the executor to be used to
54                schedule the new thread of execution.
55              </p></dd>
56<dt><span class="term">a</span></dt>
57<dd><p>
58                The <a class="link" href="../awaitable.html" title="awaitable"><code class="computeroutput">awaitable</code></a>
59                object that is the result of calling the coroutine's entry point
60                function.
61              </p></dd>
62<dt><span class="term">token</span></dt>
63<dd>
64<p>
65                The completion token that will handle the notification that the thread
66                of execution has completed. The function signature of the completion
67                handler must be:
68</p>
69<pre class="programlisting">void handler(std::exception_ptr);
70</pre>
71<p>
72              </p>
73</dd>
74</dl>
75</div>
76<h6>
77<a name="boost_asio.reference.co_spawn.overload3.h1"></a>
78          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload3.example"></a></span><a class="link" href="overload3.html#boost_asio.reference.co_spawn.overload3.example">Example</a>
79        </h6>
80<pre class="programlisting">boost::asio::awaitable&lt;std::size_t&gt; echo(tcp::socket socket)
81{
82  std::size_t bytes_transferred = 0;
83
84  try
85  {
86    char data[1024];
87    for (;;)
88    {
89      std::size_t n = co_await socket.async_read_some(
90          boost::asio::buffer(data), boost::asio::use_awaitable);
91
92      co_await boost::asio::async_write(socket,
93          boost::asio::buffer(data, n), boost::asio::use_awaitable);
94
95      bytes_transferred += n;
96    }
97  }
98  catch (const std::exception&amp;)
99  {
100  }
101
102  co_return bytes_transferred;
103}
104
105// ...
106
107boost::asio::co_spawn(my_io_context,
108  echo(std::move(my_tcp_socket)),
109  [](std::exception_ptr e, std::size_t n)
110  {
111    std::cout &lt;&lt; "transferred " &lt;&lt; n &lt;&lt; "\n";
112  });
113</pre>
114</div>
115<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
116<td align="left"></td>
117<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
118      Kohlhoff<p>
119        Distributed under the Boost Software License, Version 1.0. (See accompanying
120        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>)
121      </p>
122</div></td>
123</tr></table>
124<hr>
125<div class="spirit-nav">
126<a accesskey="p" href="overload2.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="overload4.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
127</div>
128</body>
129</html>
130