• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Stackless 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="concurrency_hint.html" title="Concurrency Hints">
10<link rel="next" href="spawn.html" title="Stackful Coroutines">
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="concurrency_hint.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="spawn.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.coroutine"></a><a class="link" href="coroutine.html" title="Stackless Coroutines">Stackless Coroutines</a>
28</h4></div></div></div>
29<p>
30          The <a class="link" href="../../reference/coroutine.html" title="coroutine"><code class="computeroutput">coroutine</code></a>
31          class provides support for stackless coroutines. Stackless coroutines enable
32          programs to implement asynchronous logic in a synchronous manner, with
33          minimal overhead, as shown in the following example:
34        </p>
35<pre class="programlisting">struct session : boost::asio::coroutine
36{
37  boost::shared_ptr&lt;tcp::socket&gt; socket_;
38  boost::shared_ptr&lt;std::vector&lt;char&gt; &gt; buffer_;
39
40  session(boost::shared_ptr&lt;tcp::socket&gt; socket)
41    : socket_(socket),
42      buffer_(new std::vector&lt;char&gt;(1024))
43  {
44  }
45
46  void operator()(boost::system::error_code ec = boost::system::error_code(), std::size_t n = 0)
47  {
48    if (!ec) reenter (this)
49    {
50      for (;;)
51      {
52        yield socket_-&gt;async_read_some(boost::asio::buffer(*buffer_), *this);
53        yield boost::asio::async_write(*socket_, boost::asio::buffer(*buffer_, n), *this);
54      }
55    }
56  }
57};
58</pre>
59<p>
60          The <code class="computeroutput">coroutine</code> class is used in conjunction with the pseudo-keywords
61          <code class="computeroutput">reenter</code>, <code class="computeroutput">yield</code> and <code class="computeroutput">fork</code>. These are
62          preprocessor macros, and are implemented in terms of a <code class="computeroutput">switch</code>
63          statement using a technique similar to Duff's Device. The <a class="link" href="../../reference/coroutine.html" title="coroutine"><code class="computeroutput">coroutine</code></a>
64          class's documentation provides a complete description of these pseudo-keywords.
65        </p>
66<h6>
67<a name="boost_asio.overview.core.coroutine.h0"></a>
68          <span class="phrase"><a name="boost_asio.overview.core.coroutine.see_also"></a></span><a class="link" href="coroutine.html#boost_asio.overview.core.coroutine.see_also">See
69          Also</a>
70        </h6>
71<p>
72          <a class="link" href="../../reference/coroutine.html" title="coroutine">coroutine</a>, <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.http_server_4">HTTP Server
73          4 example</a>, <a class="link" href="spawn.html" title="Stackful Coroutines">Stackful
74          Coroutines</a>.
75        </p>
76</div>
77<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
78<td align="left"></td>
79<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
80      Kohlhoff<p>
81        Distributed under the Boost Software License, Version 1.0. (See accompanying
82        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>)
83      </p>
84</div></td>
85</tr></table>
86<hr>
87<div class="spirit-nav">
88<a accesskey="p" href="concurrency_hint.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="spawn.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
89</div>
90</body>
91</html>
92