• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Timers</title>
5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
8<link rel="up" href="../overview.html" title="Overview">
9<link rel="prev" href="networking/bsd_sockets.html" title="The BSD Socket API and Boost.Asio">
10<link rel="next" href="serial_ports.html" title="Serial Ports">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
15<td align="center"><a href="../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="networking/bsd_sockets.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="serial_ports.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_asio.overview.timers"></a><a class="link" href="timers.html" title="Timers">Timers</a>
28</h3></div></div></div>
29<p>
30        Long running I/O operations will often have a deadline by which they must
31        have completed. These deadlines may be expressed as absolute times, but are
32        often calculated relative to the current time.
33      </p>
34<p>
35        As a simple example, to perform a synchronous wait operation on a timer using
36        a relative time one may write:
37      </p>
38<pre class="programlisting">io_context i;
39...
40deadline_timer t(i);
41t.expires_from_now(boost::posix_time::seconds(5));
42t.wait();
43</pre>
44<p>
45        More commonly, a program will perform an asynchronous wait operation on a
46        timer:
47      </p>
48<pre class="programlisting">void handler(boost::system::error_code ec) { ... }
49...
50io_context i;
51...
52deadline_timer t(i);
53t.expires_from_now(boost::posix_time::milliseconds(400));
54t.async_wait(handler);
55...
56i.run();
57</pre>
58<p>
59        The deadline associated with a timer may also be obtained as a relative time:
60      </p>
61<pre class="programlisting">boost::posix_time::time_duration time_until_expiry
62  = t.expires_from_now();
63</pre>
64<p>
65        or as an absolute time to allow composition of timers:
66      </p>
67<pre class="programlisting">deadline_timer t2(i);
68t2.expires_at(t.expires_at() + boost::posix_time::seconds(30));
69</pre>
70<h5>
71<a name="boost_asio.overview.timers.h0"></a>
72        <span class="phrase"><a name="boost_asio.overview.timers.see_also"></a></span><a class="link" href="timers.html#boost_asio.overview.timers.see_also">See
73        Also</a>
74      </h5>
75<p>
76        <a class="link" href="../reference/basic_deadline_timer.html" title="basic_deadline_timer">basic_deadline_timer</a>,
77        <a class="link" href="../reference/deadline_timer.html" title="deadline_timer">deadline_timer</a>,
78        <a class="link" href="../tutorial/tuttimer1.html" title="Timer.1 - Using a timer synchronously">timer tutorials</a>.
79      </p>
80</div>
81<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
82<td align="left"></td>
83<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
84      Kohlhoff<p>
85        Distributed under the Boost Software License, Version 1.0. (See accompanying
86        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
87      </p>
88</div></td>
89</tr></table>
90<hr>
91<div class="spirit-nav">
92<a accesskey="p" href="networking/bsd_sockets.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="serial_ports.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
93</div>
94</body>
95</html>
96