Coroutines
Boost.Asio has also included support for coroutines since version 1.56.0 and supported native coroutines since version 1.75.0.
As we have learned in the previous chapter, using coroutines simplifies how the program is written as there is no need to add completion handlers and split the flow of the program into different asynchronous functions and callbacks. Instead, with coroutines, the program follows a sequential structure where an asynchronous operation call pauses the execution of the coroutine. When the asynchronous operation completes, the coroutine is resumed, letting the program continue its execution from where it was previously paused.
With newer versions (newer than 1.75.0), we can use native C++ coroutines via co_await
, to wait for asynchronous operations within a coroutine, boost::asio::co_spawn
to launch a coroutine, and boost::asio::use_awaitable
to let Boost.Asio know that an asynchronous operation will use coroutines. With earlier versions (from 1...