Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Timer.6 - Using completion tokens

This tutorial program returns to the asynchronous wait from tutorial Timer.2, to show that a completion handler is only one of the ways in which the result of an asynchronous operation may be delivered. The program should execute identically to the tutorial program from tutorial Timer.2.

#include <iostream>
#include <boost/asio.hpp>

As in tutorial Timer.2, we define a function called print to be called when the asynchronous wait finishes.

void print(const boost::system::error_code& /*e*/)
{
  std::cout << "Hello, world!" << std::endl;
}

int main()
{
  boost::asio::io_context io;

  boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));

In tutorial Timer.2 we learnt that using asio's asynchronous functionality means supplying a completion token. The completion token is always the final argument to the initiating function, and it determines both how the result is delivered and what the initiating function returns. When we passed the print function to steady_timer::async_wait(), the completion token was the completion handler itself.

In this program we pass the boost::asio::eager completion token instead. The wait is still started immediately, but steady_timer::async_wait() now returns an object that represents the wait in progress. The result of the wait is held until we supply a completion handler.

  auto op = t.async_wait(boost::asio::eager);

Next we invoke the returned object with the same print function. The object may only be invoked once, and it must be invoked as an rvalue, so we use std::move. Nothing happens between the two calls in this program, but other work could be done there while the wait is in progress.

  std::move(op)(&print);

Had we passed boost::asio::deferred instead, the call to steady_timer::async_wait() would not have started the wait. It would instead return an object that starts the wait when it is invoked with a completion token.

There are many other kinds of completion token, allowing the result to be delivered to a future, or to be waited for by a coroutine, for example. There are also completion_token_adapters, which wrap another completion token to modify its behaviour, such as to deliver an error into a boost::system::error_code variable instead of throwing an exception, or to add a timeout to the operation.

Most asynchronous operations in asio have a default completion token, which is used when the completion token argument is omitted. This is normally boost::asio::deferred, so a call written as t.async_wait() returns a deferred operation. In tutorial Timer.7 you will see that a coroutine is able to wait for such an operation directly.

Finally, we call the boost::asio::io_context::run() member function.

  io.run();

  return 0;
}

See the full source listing

Return to the tutorial index

Previous: Timer.5 - Synchronising completion handlers in multithreaded programs

Next: Timer.7 - Using a coroutine to implement a repeating timer


PrevUpHomeNext