Insane_DNS/libraries/asio-1.28.1/doc/asio/overview/timers.html

87 lines
6.7 KiB
HTML
Raw Normal View History

2023-10-24 21:59:30 -04:00
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Timers</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../../index.html" title="Asio">
<link rel="up" href="../overview.html" title="Overview">
<link rel="prev" href="networking/bsd_sockets.html" title="The BSD Socket API and Asio">
<link rel="next" href="files.html" title="Files">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="networking/bsd_sockets.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="files.html"><img src="../../next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="asio.overview.timers"></a><a class="link" href="timers.html" title="Timers">Timers</a>
</h3></div></div></div>
<p>
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.
</p>
<p>
As a simple example, to perform a synchronous wait operation on a timer using
a relative time one may write:
</p>
<pre class="programlisting"><span class="identifier">io_context</span> <span class="identifier">i</span><span class="special">;</span>
<span class="special">...</span>
<span class="identifier">steady_timer</span> <span class="identifier">t</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
<span class="identifier">t</span><span class="special">.</span><span class="identifier">expires_after</span><span class="special">(</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">5</span><span class="special">));</span>
<span class="identifier">t</span><span class="special">.</span><span class="identifier">wait</span><span class="special">();</span>
</pre>
<p>
More commonly, a program will perform an asynchronous wait operation on a
timer:
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="special">...</span> <span class="special">}</span>
<span class="special">...</span>
<span class="identifier">io_context</span> <span class="identifier">i</span><span class="special">;</span>
<span class="special">...</span>
<span class="identifier">steady_timer</span> <span class="identifier">t</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
<span class="identifier">t</span><span class="special">.</span><span class="identifier">expires_after</span><span class="special">(</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">milliseconds</span><span class="special">(</span><span class="number">400</span><span class="special">));</span>
<span class="identifier">t</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">handler</span><span class="special">);</span>
<span class="special">...</span>
<span class="identifier">i</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
</pre>
<p>
The deadline associated with a timer may also be obtained as an absolute
time:
</p>
<pre class="programlisting"><span class="identifier">steady_timer</span><span class="special">::</span><span class="identifier">time_point</span> <span class="identifier">time_of_expiry</span> <span class="special">=</span> <span class="identifier">t</span><span class="special">.</span><span class="identifier">expiry</span><span class="special">();</span>
</pre>
<p>
which allows composition of timers:
</p>
<pre class="programlisting"><span class="identifier">steady_timer</span> <span class="identifier">t2</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
<span class="identifier">t2</span><span class="special">.</span><span class="identifier">expires_at</span><span class="special">(</span><span class="identifier">t</span><span class="special">.</span><span class="identifier">expiry</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">chrono</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">30</span><span class="special">));</span>
</pre>
<h5>
<a name="asio.overview.timers.h0"></a>
<span><a name="asio.overview.timers.see_also"></a></span><a class="link" href="timers.html#asio.overview.timers.see_also">See
Also</a>
</h5>
<p>
<a class="link" href="../reference/basic_waitable_timer.html" title="basic_waitable_timer">basic_waitable_timer</a>,
<a class="link" href="../reference/steady_timer.html" title="steady_timer">steady_timer</a>, <a class="link" href="../reference/system_timer.html" title="system_timer">system_timer</a>,
<a class="link" href="../reference/high_resolution_timer.html" title="high_resolution_timer">high_resolution_timer</a>,
<a class="link" href="../tutorial/tuttimer1.html" title="Timer.1 - Using a timer synchronously">timer tutorials</a>.
</p>
</div>
<div class="copyright-footer">Copyright © 2003-2023 Christopher M. Kohlhoff<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="networking/bsd_sockets.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="files.html"><img src="../../next.png" alt="Next"></a>
</div>
</body>
</html>