Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

spawn

Start a new stackful coroutine.

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

template<
    typename Executor,
    typename F,
    typename CompletionToken = default_completion_token_t<Executor>>
auto spawn(
    const Executor & ex,
    F && function,
    CompletionToken && token = default_completion_token_t< Executor >(),
    constraint_t< is_executor< Executor >::value||execution::is_executor< Executor >::value >  = 0);
  » more...

Start a new stackful coroutine that executes on a given execution context.

template<
    typename ExecutionContext,
    typename F,
    typename CompletionToken = default_completion_token_t<            typename ExecutionContext::executor_type>>
auto spawn(
    ExecutionContext & ctx,
    F && function,
    CompletionToken && token = default_completion_token_t< typename ExecutionContext::executor_type >(),
    constraint_t< is_convertible< ExecutionContext &, execution_context & >::value >  = 0);
  » more...

Start a new stackful coroutine, inheriting the executor of another.

template<
    typename Executor,
    typename F,
    typename CompletionToken = default_completion_token_t<Executor>>
auto spawn(
    const basic_yield_context< Executor > & ctx,
    F && function,
    CompletionToken && token = default_completion_token_t< Executor >(),
    constraint_t< is_executor< Executor >::value||execution::is_executor< Executor >::value >  = 0);
  » more...

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

template<
    typename Executor,
    typename StackAllocator,
    typename F,
    typename CompletionToken = default_completion_token_t<Executor>>
auto spawn(
    const Executor & ex,
    allocator_arg_t ,
    StackAllocator && stack_allocator,
    F && function,
    CompletionToken && token = default_completion_token_t< Executor >(),
    constraint_t< is_executor< Executor >::value||execution::is_executor< Executor >::value >  = 0);
  » more...

Start a new stackful coroutine that executes on a given execution context.

template<
    typename ExecutionContext,
    typename StackAllocator,
    typename F,
    typename CompletionToken = default_completion_token_t<            typename ExecutionContext::executor_type>>
auto spawn(
    ExecutionContext & ctx,
    allocator_arg_t ,
    StackAllocator && stack_allocator,
    F && function,
    CompletionToken && token = default_completion_token_t< typename ExecutionContext::executor_type >(),
    constraint_t< is_convertible< ExecutionContext &, execution_context & >::value >  = 0);
  » more...

Start a new stackful coroutine, inheriting the executor of another.

template<
    typename Executor,
    typename StackAllocator,
    typename F,
    typename CompletionToken = default_completion_token_t<Executor>>
auto spawn(
    const basic_yield_context< Executor > & ctx,
    allocator_arg_t ,
    StackAllocator && stack_allocator,
    F && function,
    CompletionToken && token = default_completion_token_t< Executor >(),
    constraint_t< is_executor< Executor >::value||execution::is_executor< Executor >::value >  = 0);
  » more...

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine, calling the specified handler when it completes.

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

template<
    typename Handler,
    typename Function>
void spawn(
    Handler && handler,
    Function && function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes(),
    constraint_t< !is_executor< decay_t< Handler > >::value &&!execution::is_executor< decay_t< Handler > >::value &&!is_convertible< Handler &, execution_context & >::value >  = 0);
  » more...

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine, inheriting the execution context of another.

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

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine that executes on a given executor.

template<
    typename Function,
    typename Executor>
void spawn(
    const Executor & ex,
    Function && function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes(),
    constraint_t< is_executor< Executor >::value||execution::is_executor< Executor >::value >  = 0);
  » more...

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine that executes on a given strand.

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

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine that executes in the context of a strand.

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

(Deprecated: Use overloads with a completion token.) Start a new stackful coroutine that executes on a given execution context.

template<
    typename Function,
    typename ExecutionContext>
void spawn(
    ExecutionContext & ctx,
    Function && function,
    const boost::coroutines::attributes & attributes = boost::coroutines::attributes(),
    constraint_t< is_convertible< ExecutionContext &, execution_context & >::value >  = 0);
  » 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:

boost::asio::spawn(my_strand, do_echo, boost::asio::detached);

// ...

void do_echo(boost::asio::yield_context yield)
{
  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)
  {
    // ...
  }
}
Requirements

Header: boost/asio/impl/spawn.hpp

Convenience header: boost/asio.hpp


PrevUpHomeNext