Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Timers

Long running I/O operations will often have a deadline by which they must have completed. These deadlines may be expressed as absolute times, but are often calculated relative to the current time.

As a simple example, to perform a synchronous wait operation on a timer using a relative time one may write:

io_context i;
...
steady_timer t(i);
t.expires_after(chrono::seconds(5));
t.wait();

More commonly, a program will perform an asynchronous wait operation on a timer:

void handler(boost::system::error_code ec) { ... }
...
io_context i;
...
steady_timer t(i);
t.expires_after(chrono::milliseconds(400));
t.async_wait(handler);
...
i.run();

The deadline associated with a timer may also be obtained as an absolute time:

steady_timer::time_point time_of_expiry = t.expiry();

which allows composition of timers:

steady_timer t2(i);
t2.expires_at(t.expiry() + chrono::seconds(30));
See Also

basic_waitable_timer, steady_timer, system_timer, high_resolution_timer, timer tutorials.


PrevUpHomeNext