• 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 (5 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="overload4.html" title="co_spawn (4 of 6 overloads)">
10<link rel="next" href="overload6.html" title="co_spawn (6 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="overload4.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="overload6.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.overload5"></a><a class="link" href="overload5.html" title="co_spawn (5 of 6 overloads)">co_spawn (5
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 <a class="link" href="../Executor1.html" title="Executor requirements">Executor</a>,
35    typename F,
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    const Executor &amp; ex,
39    F &amp;&amp; f,
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_executor&lt; Executor &gt;::value||execution::is_executor&lt; Executor &gt;::value &gt;::type *  = 0);
42</pre>
43<h6>
44<a name="boost_asio.reference.co_spawn.overload5.h0"></a>
45          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload5.parameters"></a></span><a class="link" href="overload5.html#boost_asio.reference.co_spawn.overload5.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">ex</span></dt>
51<dd><p>
52                The executor that will be used to schedule the new thread of execution.
53              </p></dd>
54<dt><span class="term">f</span></dt>
55<dd><p>
56                A nullary function object with a return type of the form <code class="computeroutput">boost::asio::awaitable&lt;R,E&gt;</code>
57                that will be used as the coroutine's entry point.
58              </p></dd>
59<dt><span class="term">token</span></dt>
60<dd><p>
61                The completion token that will handle the notification that the thread
62                of execution has completed. If <code class="computeroutput">R</code> is <code class="computeroutput">void</code>,
63                the function signature of the completion handler must be:
64              </p></dd>
65</dl>
66</div>
67<pre class="programlisting">void handler(std::exception_ptr);
68</pre>
69<p>
70          Otherwise, the function signature of the completion handler must be:
71        </p>
72<pre class="programlisting">void handler(std::exception_ptr, R);
73</pre>
74<h6>
75<a name="boost_asio.reference.co_spawn.overload5.h1"></a>
76          <span class="phrase"><a name="boost_asio.reference.co_spawn.overload5.example"></a></span><a class="link" href="overload5.html#boost_asio.reference.co_spawn.overload5.example">Example</a>
77        </h6>
78<pre class="programlisting">boost::asio::awaitable&lt;std::size_t&gt; echo(tcp::socket socket)
79{
80  std::size_t bytes_transferred = 0;
81
82  try
83  {
84    char data[1024];
85    for (;;)
86    {
87      std::size_t n = co_await socket.async_read_some(
88          boost::asio::buffer(data), boost::asio::use_awaitable);
89
90      co_await boost::asio::async_write(socket,
91          boost::asio::buffer(data, n), boost::asio::use_awaitable);
92
93      bytes_transferred += n;
94    }
95  }
96  catch (const std::exception&amp;)
97  {
98  }
99
100  co_return bytes_transferred;
101}
102
103// ...
104
105boost::asio::co_spawn(my_executor,
106  [socket = std::move(my_tcp_socket)]() mutable
107    -&gt; boost::asio::awaitable&lt;void&gt;
108  {
109    try
110    {
111      char data[1024];
112      for (;;)
113      {
114        std::size_t n = co_await socket.async_read_some(
115            boost::asio::buffer(data), boost::asio::use_awaitable);
116
117        co_await boost::asio::async_write(socket,
118            boost::asio::buffer(data, n), boost::asio::use_awaitable);
119      }
120    }
121    catch (const std::exception&amp; e)
122    {
123      std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
124    }
125  }, boost::asio::detached);
126</pre>
127</div>
128<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
129<td align="left"></td>
130<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
131      Kohlhoff<p>
132        Distributed under the Boost Software License, Version 1.0. (See accompanying
133        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>)
134      </p>
135</div></td>
136</tr></table>
137<hr>
138<div class="spirit-nav">
139<a accesskey="p" href="overload4.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="overload6.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
140</div>
141</body>
142</html>
143