2 / Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
4 / Distributed under the Boost Software License, Version 1.0. (See accompanying
5 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 [section:coroutine Stackless Coroutines]
10 The [link boost_asio.reference.coroutine `coroutine`] class provides support for
11 stackless coroutines. Stackless coroutines enable programs to implement
12 asynchronous logic in a synchronous manner, with minimal overhead, as shown in
13 the following example:
15 struct session : boost::asio::coroutine
17 boost::shared_ptr<tcp::socket> socket_;
18 boost::shared_ptr<std::vector<char> > buffer_;
20 session(boost::shared_ptr<tcp::socket> socket)
22 buffer_(new std::vector<char>(1024))
26 void operator()(boost::system::error_code ec = boost::system::error_code(), std::size_t n = 0)
28 if (!ec) reenter (this)
32 yield socket_->async_read_some(boost::asio::buffer(*buffer_), *this);
33 yield boost::asio::async_write(*socket_, boost::asio::buffer(*buffer_, n), *this);
39 The `coroutine` class is used in conjunction with the pseudo-keywords
40 `reenter`, `yield` and `fork`. These are preprocessor macros, and are
41 implemented in terms of a `switch` statement using a technique similar to
42 Duff's Device. The [link boost_asio.reference.coroutine `coroutine`] class's
43 documentation provides a complete description of these pseudo-keywords.
47 [link boost_asio.reference.coroutine coroutine],
48 [link boost_asio.examples.cpp03_examples.http_server_4 HTTP Server 4 example],
49 [link boost_asio.overview.core.spawn Stackful Coroutines].