![]() |
Home | Libraries | People | FAQ | More |
In this tutorial we will rewrite the repeating timer from tutorial Timer.3 as a coroutine. The program should execute identically to the tutorial program from tutorial Timer.3. This tutorial program requires a compiler that supports C++20 coroutines.
In tutorials Timer.3 to Timer.5 each completion handler started the next asynchronous wait, so the timer and the counter had to be stored outside the handler. In tutorial Timer.3 we bound pointers to them into the handler, and in tutorial Timer.4 we made them members of a class. In a coroutine they can be local variables, and the repeated wait becomes a loop.
#include <iostream> #include <boost/asio.hpp>
We start by defining a coroutine called print.
It does not produce a value, so its return type is boost::asio::awaitable<void>.
boost::asio::awaitable<void> print() {
The timer is now a local variable of the coroutine. To construct it we need an executor. We obtain the executor that the coroutine is running on by awaiting boost::asio::this_coro::executor.
boost::asio::steady_timer t(co_await boost::asio::this_coro::executor, boost::asio::chrono::seconds(1));
The counter is also a local variable. We then wait for the timer to expire for the first time.
In tutorial Timer.6 we saw that, when the completion token is omitted, steady_timer::async_wait()
returns a deferred operation. The coroutine waits for this operation using
co_await, and is suspended
until the wait completes. If the wait fails, the error is thrown as an exception.
int count = 0; co_await t.async_wait();
As in tutorial Timer.3, the program stops when the timer fires for the sixth time.
while (count < 5) {
Inside the loop we print and increment the counter, move the expiry time along by one second, and wait for the timer again. When the wait completes, the coroutine continues with the next iteration of the loop.
std::cout << count << std::endl; ++count; t.expires_at(t.expiry() + boost::asio::chrono::seconds(1)); co_await t.async_wait(); }
You will note that the final value of the counter is now printed from within the coroutine, since the counter is one of its local variables.
std::cout << "Final count is " << count << std::endl; }
The main function starts
the coroutine by calling boost::asio::co_spawn(). The first argument is the
io_context, which provides the executor that the coroutine will run on. The
second argument is the result of calling print,
which creates the coroutine without running it.
The boost::asio::co_spawn() function is itself an initiating function, so its final argument is a completion token. This token is used when the coroutine finishes. We are not interested in the result, so we pass boost::asio::detached.
int main() { boost::asio::io_context io; boost::asio::co_spawn(io, print(), boost::asio::detached);
The coroutine counts as "work" until it finishes, so the call to boost::asio::io_context::run() will not return until the loop has ended.
io.run(); return 0; }
See the full source listing
Return to the tutorial index
Previous: Timer.6 - Using completion tokens
Next: Timer.8 - Synchronising coroutines in multithreaded programs