Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::asio::spawn

Start a new stackful coroutine. More...

Functions

template<typename Handler , typename Function >
void boost::asio::spawn (BOOST_ASIO_MOVE_ARG(Handler) handler, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes &attributes=boost::coroutines::attributes())
 Start a new stackful coroutine, calling the specified handler when it completes. More...
 
template<typename Handler , typename Function >
void boost::asio::spawn (basic_yield_context< Handler > ctx, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes &attributes=boost::coroutines::attributes())
 Start a new stackful coroutine, inheriting the execution context of another. More...
 
template<typename Function >
void boost::asio::spawn (boost::asio::io_service::strand strand, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes &attributes=boost::coroutines::attributes())
 Start a new stackful coroutine that executes in the context of a strand. More...
 
template<typename Function >
void boost::asio::spawn (boost::asio::io_service &io_service, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes &attributes=boost::coroutines::attributes())
 Start a new stackful coroutine that executes on a given io_service. More...
 

Detailed Description

Start a new stackful coroutine.

The spawn() function is a high-level wrapper over the Boost.Coroutine library. This function enables programs to implement asynchronous logic in a synchronous manner, as illustrated by the following example:

boost::asio::spawn(my_strand, do_echo);
// ...
{
try
{
char data[128];
for (;;)
{
std::size_t length =
my_socket.async_read_some(
boost::asio::buffer(data), yield);
boost::asio::async_write(my_socket,
boost::asio::buffer(data, length), yield);
}
}
catch (std::exception& e)
{
// ...
}
}

Function Documentation

template<typename Handler , typename Function >
void boost::asio::spawn ( BOOST_ASIO_MOVE_ARG(Handler)  handler,
BOOST_ASIO_MOVE_ARG(Function)  function,
const boost::coroutines::attributes attributes = boost::coroutines::attributes() 
)

#include <boost_1_57_0/boost/asio/spawn.hpp>

Start a new stackful coroutine, calling the specified handler when it completes.

This function is used to launch a new coroutine.

Parameters
handlerA handler to be called when the coroutine exits. More importantly, the handler provides an execution context (via the the handler invocation hook) for the coroutine. The handler must have the signature:
void handler();
functionThe coroutine function. The function must have the signature:
void function(basic_yield_context<Handler> yield);
attributesBoost.Coroutine attributes used to customise the coroutine.
template<typename Handler , typename Function >
void boost::asio::spawn ( basic_yield_context< Handler >  ctx,
BOOST_ASIO_MOVE_ARG(Function)  function,
const boost::coroutines::attributes attributes = boost::coroutines::attributes() 
)

#include <boost_1_57_0/boost/asio/spawn.hpp>

Start a new stackful coroutine, inheriting the execution context of another.

This function is used to launch a new coroutine.

Parameters
ctxIdentifies the current coroutine as a parent of the new coroutine. This specifies that the new coroutine should inherit the execution context of the parent. For example, if the parent coroutine is executing in a particular strand, then the new coroutine will execute in the same strand.
functionThe coroutine function. The function must have the signature:
void function(basic_yield_context<Handler> yield);
attributesBoost.Coroutine attributes used to customise the coroutine.
template<typename Function >
void boost::asio::spawn ( boost::asio::io_service::strand  strand,
BOOST_ASIO_MOVE_ARG(Function)  function,
const boost::coroutines::attributes attributes = boost::coroutines::attributes() 
)

#include <boost_1_57_0/boost/asio/spawn.hpp>

Start a new stackful coroutine that executes in the context of a strand.

This function is used to launch a new coroutine.

Parameters
strandIdentifies a strand. By starting multiple coroutines on the same strand, the implementation ensures that none of those coroutines can execute simultaneously.
functionThe coroutine function. The function must have the signature:
void function(yield_context yield);
attributesBoost.Coroutine attributes used to customise the coroutine.
template<typename Function >
void boost::asio::spawn ( boost::asio::io_service io_service,
BOOST_ASIO_MOVE_ARG(Function)  function,
const boost::coroutines::attributes attributes = boost::coroutines::attributes() 
)

#include <boost_1_57_0/boost/asio/spawn.hpp>

Start a new stackful coroutine that executes on a given io_service.

This function is used to launch a new coroutine.

Parameters
io_serviceIdentifies the io_service that will run the coroutine. The new coroutine is implicitly given its own strand within this io_service.
functionThe coroutine function. The function must have the signature:
void function(yield_context yield);
attributesBoost.Coroutine attributes used to customise the coroutine.