asio C++ library

PrevUpHomeNext

spawn

Start a new stackful coroutine.

template<
    typename Handler,
    typename Function>
void spawn(
    Handler handler,
    Function function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes());
  » more...

template<
    typename Handler,
    typename Function>
void spawn(
    basic_yield_context< Handler > ctx,
    Function function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes());
  » more...

template<
    typename Function>
void spawn(
    asio::io_service::strand strand,
    Function function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes());
  » more...

template<
    typename Function>
void spawn(
    asio::io_service & io_service,
    Function function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes());
  » more...

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:

asio::spawn(my_strand, do_echo);

// ...

void do_echo(asio::yield_context yield)
{
  try
  {
    char data[128];
    for (;;)
    {
      std::size_t length =
        my_socket.async_read_some(
          asio::buffer(data), yield);

      asio::async_write(my_socket,
          asio::buffer(data, length), yield);
    }
  }
  catch (std::exception& e)
  {
    // ...
  }
}
Requirements

Header: asio/spawn.hpp

Convenience header: None


PrevUpHomeNext