Boost C++ 库

...世界上最受推崇且设计最精良的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ 编码规范

spawn - Boost C++ 函数库
PrevUpHomeNext

启动一个新的有栈协程。

启动一个新的有栈协程,使其在给定的执行器上执行。

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...

启动一个新的有栈协程,使其在给定的执行上下文中执行。

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...

启动一个新的有栈协程,继承另一个协程的执行器。

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...

启动一个新的有栈协程,使其在给定的执行器上执行。

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...

启动一个新的有栈协程,使其在给定的执行上下文中执行。

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...

启动一个新的有栈协程,继承另一个协程的执行器。

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...

spawn 函数是对 Boost.Coroutine 库的高级封装。 该函数使程序能够以同步方式实现异步逻辑,如下面的示例所示

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)
  {
    // ...
  }
}
要求

头文件: boost/asio/impl/spawn.hpp

便捷头文件: boost/asio.hpp


PrevUpHomeNext