coroutines_ts.qbk 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:coroutines_ts Coroutines TS Support]
  8. Support for the Coroutines TS is provided via the [link
  9. boost_asio.reference.awaitable `awaitable`] class template, the [link
  10. boost_asio.reference.use_awaitable_t `use_awaitable`] completion token, and the [link
  11. boost_asio.reference.co_spawn `co_spawn()`] function. These facilities allow programs
  12. to implement asynchronous logic in a synchronous manner, in conjunction with
  13. the `co_await` keyword, as shown in the following example:
  14. boost::asio::co_spawn(executor,
  15. [socket = std::move(socket)]() mutable
  16. {
  17. return echo(std::move(socket));
  18. },
  19. boost::asio::detached);
  20. // ...
  21. boost::asio::awaitable<void> echo(tcp::socket socket)
  22. {
  23. try
  24. {
  25. char data[1024];
  26. for (;;)
  27. {
  28. std::size_t n = co_await socket.async_read_some(boost::asio::buffer(data), boost::asio::use_awaitable);
  29. co_await async_write(socket, boost::asio::buffer(data, n), boost::asio::use_awaitable);
  30. }
  31. }
  32. catch (std::exception& e)
  33. {
  34. std::printf("echo Exception: %s\n", e.what());
  35. }
  36. }
  37. The first argument to `co_spawn()` is an [link boost_asio.reference.Executor1
  38. executor] that determines the context in which the coroutine is permitted to
  39. execute. For example, a server's per-client object may consist of multiple
  40. coroutines; they should all run on the same `strand` so that no explicit
  41. synchronisation is required.
  42. The second argument is a nullary function object that returns a [link
  43. boost_asio.reference.awaitable `boost::asio::awaitable<R>`],
  44. where `R` is the type of return value produced by the coroutine. In the above
  45. example, the coroutine returns `void`.
  46. The third argument is a completion token, and this is used by `co_spawn()` to
  47. produce a completion handler with signature `void(std::exception_ptr, R)`. This
  48. completion handler is invoked with the result of the coroutine once it has
  49. finished. In the above example we pass a completion token type, [link
  50. boost_asio.reference.detached `boost::asio::detached`], which is used to explicitly ignore
  51. the result of an asynchronous operation.
  52. In this example the body of the coroutine is implemented in the `echo`
  53. function. When the `use_awaitable` completion token is passed to an
  54. asynchronous operation, the operation's initiating function returns an
  55. `awaitable` that may be used with the `co_await` keyword:
  56. std::size_t n = co_await socket.async_read_some(boost::asio::buffer(data), boost::asio::use_awaitable);
  57. Where an asynchronous operation's handler signature has the form:
  58. void handler(boost::system::error_code ec, result_type result);
  59. the resulting type of the `co_await` expression is `result_type`. In the
  60. `async_read_some` example above, this is `size_t`. If the asynchronous
  61. operation fails, the `error_code` is converted into a `system_error` exception
  62. and thrown.
  63. Where a handler signature has the form:
  64. void handler(boost::system::error_code ec);
  65. the `co_await` expression produces a `void` result. As above, an error is
  66. passed back to the coroutine as a `system_error` exception.
  67. [heading See Also]
  68. [link boost_asio.reference.co_spawn co_spawn],
  69. [link boost_asio.reference.detached detached],
  70. [link boost_asio.reference.redirect_error redirect_error],
  71. [link boost_asio.reference.awaitable awaitable],
  72. [link boost_asio.reference.use_awaitable_t use_awaitable_t],
  73. [link boost_asio.reference.use_awaitable use_awaitable],
  74. [link boost_asio.reference.this_coro__executor this_coro::executor],
  75. [link boost_asio.examples.cpp17_examples.coroutines_ts_support Coroutines TS examples],
  76. [link boost_asio.overview.core.spawn Stackful Coroutines],
  77. [link boost_asio.overview.core.coroutine Stackless Coroutines].
  78. [endsect]