tuttimer1.html 5.04 KB
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Timer.1 - Using a timer synchronously</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="../tutorial.html" title="Tutorial">
<link rel="prev" href="../tutorial.html" title="Tutorial">
<link rel="next" href="tuttimer1/src.html" title="Source listing for Timer.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="../tutorial.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tuttimer1/src.html"><img src="../../next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="asio.tutorial.tuttimer1"></a><a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - Using a timer synchronously</a>
</h3></div></div></div>
<p>
        This tutorial program introduces asio by showing how to perform a blocking
        wait on a timer.
      </p>
<p>
        We start by including the necessary header files.
      </p>
<p>
        All of the asio classes can be used by simply including the <code class="computeroutput">"asio.hpp"</code>
        header file.
      </p>
<pre class="programlisting">#include &lt;iostream&gt;
#include &lt;asio.hpp&gt;
</pre>
<p>
        All programs that use asio need to have at least one I/O execution context,
        such as an <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> or
        <a class="link" href="../reference/thread_pool.html" title="thread_pool">thread_pool</a> object. An
        I/O execution context provides access to I/O functionality. We declare an
        object of type <a class="link" href="../reference/io_context.html" title="io_context">io_context</a>
        first thing in the main function.
      </p>
<pre class="programlisting">int main()
{
  asio::io_context io;
</pre>
<p>
        Next we declare an object of type asio::steady_timer. The core asio classes
        that provide I/O functionality (or as in this case timer functionality) always
        take a reference to an io_context as their first constructor argument. The
        second argument to the constructor sets the timer to expire 5 seconds from
        now.
      </p>
<pre class="programlisting">  asio::steady_timer t(io, asio::chrono::seconds(5));
</pre>
<p>
        In this simple example we perform a blocking wait on the timer. That is,
        the call to <a class="link" href="../reference/basic_waitable_timer/wait.html" title="basic_waitable_timer::wait">steady_timer::wait()</a>
        will not return until the timer has expired, 5 seconds after it was created
        (i.e. not from when the wait starts).
      </p>
<p>
        A timer is always in one of two states: "expired" or "not
        expired". If the <a class="link" href="../reference/basic_waitable_timer/wait.html" title="basic_waitable_timer::wait">steady_timer::wait()</a>
        function is called on an expired timer, it will return immediately.
      </p>
<pre class="programlisting">  t.wait();
</pre>
<p>
        Finally we print the obligatory <code class="computeroutput">"Hello, world!"</code> message
        to show when the timer has expired.
      </p>
<pre class="programlisting">  std::cout &lt;&lt; "Hello, world!" &lt;&lt; std::endl;

  return 0;
}
</pre>
<p>
        See the <a class="link" href="tuttimer1/src.html" title="Source listing for Timer.1">full source listing</a>
      </p>
<p>
        Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a>
      </p>
<p>
        Next: <a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - Using a timer asynchronously</a>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2020 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></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tuttimer1/src.html"><img src="../../next.png" alt="Next"></a>
</div>
</body>
</html>