1[/
2 / Copyright (c) 2003-2021 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
8[section:timers Timers]
9
10Long running I/O operations will often have a deadline by which they must have
11completed. These deadlines may be expressed as absolute times, but are often
12calculated relative to the current time.
13
14As a simple example, to perform a synchronous wait operation on a timer using a
15relative time one may write:
16
17  io_context i;
18  ...
19  deadline_timer t(i);
20  t.expires_from_now(boost::posix_time::seconds(5));
21  t.wait();
22
23More commonly, a program will perform an asynchronous wait operation on a
24timer:
25
26  void handler(boost::system::error_code ec) { ... }
27  ...
28  io_context i;
29  ...
30  deadline_timer t(i);
31  t.expires_from_now(boost::posix_time::milliseconds(400));
32  t.async_wait(handler);
33  ...
34  i.run();
35
36The deadline associated with a timer may also be obtained as a relative time:
37
38  boost::posix_time::time_duration time_until_expiry
39    = t.expires_from_now();
40
41or as an absolute time to allow composition of timers:
42
43  deadline_timer t2(i);
44  t2.expires_at(t.expires_at() + boost::posix_time::seconds(30));
45
46[heading See Also]
47
48[link boost_asio.reference.basic_deadline_timer basic_deadline_timer],
49[link boost_asio.reference.deadline_timer deadline_timer],
50[link boost_asio.tutorial.tuttimer1 timer tutorials].
51
52[endsect]
53