派生一个新的基于协程的执行线程。
template< typename Executor, typename AwaitableExecutor, typename CompletionToken = DEFAULT> DEDUCED co_spawn( const Executor & ex, awaitable< void, AwaitableExecutor > a, CompletionToken && token = DEFAULT, constraint_t<(is_executor< Executor >::value||execution::is_executor< Executor >::value) &&is_convertible< Executor, AwaitableExecutor >::value > = 0);
void(std::exception_ptr)
boost::asio::awaitable<void> echo(tcp::socket socket) { try { char data[1024]; for (;;) { std::size_t n = co_await socket.async_read_some( boost::asio::buffer(data), boost::asio::use_awaitable); co_await boost::asio::async_write(socket, boost::asio::buffer(data, n), boost::asio::use_awaitable); } } catch (const std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } } // ... boost::asio::co_spawn(my_executor, echo(std::move(my_tcp_socket)), boost::asio::detached);
新执行线程是使用仅支持 cancellation_type::terminal
值的取消状态创建的。要更改取消状态,请调用 this_coro::reset_cancellation_state
。