File: timers.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (53 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[/
 / Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
 /
 / Distributed under the Boost Software License, Version 1.0. (See accompanying
 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 /]

[section:timers 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));

[heading See Also]

[link boost_asio.reference.basic_waitable_timer basic_waitable_timer],
[link boost_asio.reference.steady_timer steady_timer],
[link boost_asio.reference.system_timer system_timer],
[link boost_asio.reference.high_resolution_timer high_resolution_timer],
[link boost_asio.tutorial.tuttimer1 timer tutorials].

[endsect]