Home | Libraries | People | FAQ | More |
Spawn a new coroutined-based thread of execution.
template< typename ExecutionContext, typename AwaitableExecutor, typename CompletionToken = DEFAULT> DEDUCED co_spawn( ExecutionContext & ctx, awaitable< void, AwaitableExecutor > a, CompletionToken && token = DEFAULT, typename enable_if< is_convertible< ExecutionContext &, execution_context & >::value &&is_convertible< typename ExecutionContext::executor_type, AwaitableExecutor >::value >::type * = 0);
An execution context that will provide the executor to be used to schedule the new thread of execution.
The awaitable
object that is the result of calling the coroutine's entry point
function.
The completion token that will handle the notification that the thread of execution has completed. The function signature of the completion handler must be:
void handler(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_io_context, echo(std::move(my_tcp_socket)), boost::asio::detached);