• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>deadline_timer</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="../reference.html" title="Reference">
9<link rel="prev" href="coroutine/is_parent.html" title="coroutine::is_parent">
10<link rel="next" href="default_completion_token.html" title="default_completion_token">
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="coroutine/is_parent.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="default_completion_token.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.reference.deadline_timer"></a><a class="link" href="deadline_timer.html" title="deadline_timer">deadline_timer</a>
28</h3></div></div></div>
29<p>
30        <a class="indexterm" name="boost_asio.indexterm.deadline_timer"></a>
31Typedef for the typical
32        usage of timer. Uses a UTC clock.
33      </p>
34<pre class="programlisting">typedef basic_deadline_timer&lt; boost::posix_time::ptime &gt; deadline_timer;
35</pre>
36<h5>
37<a name="boost_asio.reference.deadline_timer.h0"></a>
38        <span class="phrase"><a name="boost_asio.reference.deadline_timer.types"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.types">Types</a>
39      </h5>
40<div class="informaltable"><table class="table">
41<colgroup>
42<col>
43<col>
44</colgroup>
45<thead><tr>
46<th>
47                <p>
48                  Name
49                </p>
50              </th>
51<th>
52                <p>
53                  Description
54                </p>
55              </th>
56</tr></thead>
57<tbody>
58<tr>
59<td>
60                <p>
61                  <a class="link" href="basic_deadline_timer__rebind_executor.html" title="basic_deadline_timer::rebind_executor"><span class="bold"><strong>rebind_executor</strong></span></a>
62                </p>
63              </td>
64<td>
65                <p>
66                  Rebinds the timer type to another executor.
67                </p>
68              </td>
69</tr>
70<tr>
71<td>
72                <p>
73                  <a class="link" href="basic_deadline_timer/duration_type.html" title="basic_deadline_timer::duration_type"><span class="bold"><strong>duration_type</strong></span></a>
74                </p>
75              </td>
76<td>
77                <p>
78                  The duration type.
79                </p>
80              </td>
81</tr>
82<tr>
83<td>
84                <p>
85                  <a class="link" href="basic_deadline_timer/executor_type.html" title="basic_deadline_timer::executor_type"><span class="bold"><strong>executor_type</strong></span></a>
86                </p>
87              </td>
88<td>
89                <p>
90                  The type of the executor associated with the object.
91                </p>
92              </td>
93</tr>
94<tr>
95<td>
96                <p>
97                  <a class="link" href="basic_deadline_timer/time_type.html" title="basic_deadline_timer::time_type"><span class="bold"><strong>time_type</strong></span></a>
98                </p>
99              </td>
100<td>
101                <p>
102                  The time type.
103                </p>
104              </td>
105</tr>
106<tr>
107<td>
108                <p>
109                  <a class="link" href="basic_deadline_timer/traits_type.html" title="basic_deadline_timer::traits_type"><span class="bold"><strong>traits_type</strong></span></a>
110                </p>
111              </td>
112<td>
113                <p>
114                  The time traits type.
115                </p>
116              </td>
117</tr>
118</tbody>
119</table></div>
120<h5>
121<a name="boost_asio.reference.deadline_timer.h1"></a>
122        <span class="phrase"><a name="boost_asio.reference.deadline_timer.member_functions"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.member_functions">Member Functions</a>
123      </h5>
124<div class="informaltable"><table class="table">
125<colgroup>
126<col>
127<col>
128</colgroup>
129<thead><tr>
130<th>
131                <p>
132                  Name
133                </p>
134              </th>
135<th>
136                <p>
137                  Description
138                </p>
139              </th>
140</tr></thead>
141<tbody>
142<tr>
143<td>
144                <p>
145                  <a class="link" href="basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait"><span class="bold"><strong>async_wait</strong></span></a>
146                </p>
147              </td>
148<td>
149                <p>
150                  Start an asynchronous wait on the timer.
151                </p>
152              </td>
153</tr>
154<tr>
155<td>
156                <p>
157                  <a class="link" href="basic_deadline_timer/basic_deadline_timer.html" title="basic_deadline_timer::basic_deadline_timer"><span class="bold"><strong>basic_deadline_timer</strong></span></a> <span class="silver">[constructor]</span>
158                </p>
159              </td>
160<td>
161                <p>
162                  Constructor. <br> <span class="silver"> —</span><br> Constructor to set a particular expiry
163                  time as an absolute time. <br> <span class="silver"> —</span><br> Constructor to set a particular
164                  expiry time relative to now. <br> <span class="silver"> —</span><br> Move-construct a basic_deadline_timer
165                  from another.
166                </p>
167              </td>
168</tr>
169<tr>
170<td>
171                <p>
172                  <a class="link" href="basic_deadline_timer/cancel.html" title="basic_deadline_timer::cancel"><span class="bold"><strong>cancel</strong></span></a>
173                </p>
174              </td>
175<td>
176                <p>
177                  Cancel any asynchronous operations that are waiting on the timer.
178                </p>
179              </td>
180</tr>
181<tr>
182<td>
183                <p>
184                  <a class="link" href="basic_deadline_timer/cancel_one.html" title="basic_deadline_timer::cancel_one"><span class="bold"><strong>cancel_one</strong></span></a>
185                </p>
186              </td>
187<td>
188                <p>
189                  Cancels one asynchronous operation that is waiting on the timer.
190                </p>
191              </td>
192</tr>
193<tr>
194<td>
195                <p>
196                  <a class="link" href="basic_deadline_timer/expires_at.html" title="basic_deadline_timer::expires_at"><span class="bold"><strong>expires_at</strong></span></a>
197                </p>
198              </td>
199<td>
200                <p>
201                  Get the timer's expiry time as an absolute time. <br> <span class="silver"> —</span><br>
202                  Set the timer's expiry time as an absolute time.
203                </p>
204              </td>
205</tr>
206<tr>
207<td>
208                <p>
209                  <a class="link" href="basic_deadline_timer/expires_from_now.html" title="basic_deadline_timer::expires_from_now"><span class="bold"><strong>expires_from_now</strong></span></a>
210                </p>
211              </td>
212<td>
213                <p>
214                  Get the timer's expiry time relative to now. <br> <span class="silver"> —</span><br> Set
215                  the timer's expiry time relative to now.
216                </p>
217              </td>
218</tr>
219<tr>
220<td>
221                <p>
222                  <a class="link" href="basic_deadline_timer/get_executor.html" title="basic_deadline_timer::get_executor"><span class="bold"><strong>get_executor</strong></span></a>
223                </p>
224              </td>
225<td>
226                <p>
227                  Get the executor associated with the object.
228                </p>
229              </td>
230</tr>
231<tr>
232<td>
233                <p>
234                  <a class="link" href="basic_deadline_timer/operator_eq_.html" title="basic_deadline_timer::operator="><span class="bold"><strong>operator=</strong></span></a>
235                </p>
236              </td>
237<td>
238                <p>
239                  Move-assign a basic_deadline_timer from another.
240                </p>
241              </td>
242</tr>
243<tr>
244<td>
245                <p>
246                  <a class="link" href="basic_deadline_timer/wait.html" title="basic_deadline_timer::wait"><span class="bold"><strong>wait</strong></span></a>
247                </p>
248              </td>
249<td>
250                <p>
251                  Perform a blocking wait on the timer.
252                </p>
253              </td>
254</tr>
255<tr>
256<td>
257                <p>
258                  <a class="link" href="basic_deadline_timer/_basic_deadline_timer.html" title="basic_deadline_timer::~basic_deadline_timer"><span class="bold"><strong>~basic_deadline_timer</strong></span></a> <span class="silver">[destructor]</span>
259                </p>
260              </td>
261<td>
262                <p>
263                  Destroys the timer.
264                </p>
265              </td>
266</tr>
267</tbody>
268</table></div>
269<p>
270        The <a class="link" href="basic_deadline_timer.html" title="basic_deadline_timer"><code class="computeroutput">basic_deadline_timer</code></a>
271        class template provides the ability to perform a blocking or asynchronous
272        wait for a timer to expire.
273      </p>
274<p>
275        A deadline timer is always in one of two states: "expired" or "not
276        expired". If the <code class="computeroutput">wait()</code> or <code class="computeroutput">async_wait()</code> function
277        is called on an expired timer, the wait operation will complete immediately.
278      </p>
279<p>
280        Most applications will use the <a class="link" href="deadline_timer.html" title="deadline_timer"><code class="computeroutput">deadline_timer</code></a>
281        typedef.
282      </p>
283<h5>
284<a name="boost_asio.reference.deadline_timer.h2"></a>
285        <span class="phrase"><a name="boost_asio.reference.deadline_timer.thread_safety"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.thread_safety">Thread
286        Safety</a>
287      </h5>
288<p>
289        <span class="emphasis"><em>Distinct</em></span> <span class="emphasis"><em>objects:</em></span> Safe.
290      </p>
291<p>
292        <span class="emphasis"><em>Shared</em></span> <span class="emphasis"><em>objects:</em></span> Unsafe.
293      </p>
294<h5>
295<a name="boost_asio.reference.deadline_timer.h3"></a>
296        <span class="phrase"><a name="boost_asio.reference.deadline_timer.examples"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.examples">Examples</a>
297      </h5>
298<p>
299        Performing a blocking wait:
300      </p>
301<pre class="programlisting">// Construct a timer without setting an expiry time.
302boost::asio::deadline_timer timer(my_context);
303
304// Set an expiry time relative to now.
305timer.expires_from_now(boost::posix_time::seconds(5));
306
307// Wait for the timer to expire.
308timer.wait();
309</pre>
310<p>
311        Performing an asynchronous wait:
312      </p>
313<pre class="programlisting">void handler(const boost::system::error_code&amp; error)
314{
315  if (!error)
316  {
317    // Timer expired.
318  }
319}
320
321...
322
323// Construct a timer with an absolute expiry time.
324boost::asio::deadline_timer timer(my_context,
325    boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
326
327// Start an asynchronous wait.
328timer.async_wait(handler);
329</pre>
330<h5>
331<a name="boost_asio.reference.deadline_timer.h4"></a>
332        <span class="phrase"><a name="boost_asio.reference.deadline_timer.changing_an_active_deadline_timer_s_expiry_time"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.changing_an_active_deadline_timer_s_expiry_time">Changing
333        an active deadline_timer's expiry time</a>
334      </h5>
335<p>
336        Changing the expiry time of a timer while there are pending asynchronous
337        waits causes those wait operations to be cancelled. To ensure that the action
338        associated with the timer is performed only once, use something like this:
339        used:
340      </p>
341<pre class="programlisting">void on_some_event()
342{
343  if (my_timer.expires_from_now(seconds(5)) &gt; 0)
344  {
345    // We managed to cancel the timer. Start new asynchronous wait.
346    my_timer.async_wait(on_timeout);
347  }
348  else
349  {
350    // Too late, timer has already expired!
351  }
352}
353
354void on_timeout(const boost::system::error_code&amp; e)
355{
356  if (e != boost::asio::error::operation_aborted)
357  {
358    // Timer was not cancelled, take necessary action.
359  }
360}
361</pre>
362<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
363<li class="listitem">
364            The <code class="computeroutput">boost::asio::basic_deadline_timer::expires_from_now()</code>
365            function cancels any pending asynchronous waits, and returns the number
366            of asynchronous waits that were cancelled. If it returns 0 then you were
367            too late and the wait handler has already been executed, or will soon
368            be executed. If it returns 1 then the wait handler was successfully cancelled.
369          </li>
370<li class="listitem">
371            If a wait handler is cancelled, the boost::system::error_code passed
372            to it contains the value <code class="computeroutput">boost::asio::error::operation_aborted</code>.
373          </li>
374</ul></div>
375<h5>
376<a name="boost_asio.reference.deadline_timer.h5"></a>
377        <span class="phrase"><a name="boost_asio.reference.deadline_timer.requirements"></a></span><a class="link" href="deadline_timer.html#boost_asio.reference.deadline_timer.requirements">Requirements</a>
378      </h5>
379<p>
380        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/deadline_timer.hpp</code>
381      </p>
382<p>
383        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
384      </p>
385</div>
386<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
387<td align="left"></td>
388<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
389      Kohlhoff<p>
390        Distributed under the Boost Software License, Version 1.0. (See accompanying
391        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>)
392      </p>
393</div></td>
394</tr></table>
395<hr>
396<div class="spirit-nav">
397<a accesskey="p" href="coroutine/is_parent.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="default_completion_token.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
398</div>
399</body>
400</html>
401