1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Timer.1 - Using a timer synchronously</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="../tutorial.html" title="Tutorial"> 9<link rel="prev" href="../tutorial.html" title="Tutorial"> 10<link rel="next" href="tuttimer1/src.html" title="Source listing for Timer.1"> 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="../tutorial.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tuttimer1/src.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.tutorial.tuttimer1"></a><a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - Using a timer 28 synchronously</a> 29</h3></div></div></div> 30<p> 31 This tutorial program introduces asio by showing how to perform a blocking 32 wait on a timer. 33 </p> 34<p> 35 We start by including the necessary header files. 36 </p> 37<p> 38 All of the asio classes can be used by simply including the <code class="computeroutput">"asio.hpp"</code> 39 header file. 40 </p> 41<pre class="programlisting">#include <iostream> 42#include <boost/asio.hpp> 43</pre> 44<p> 45 All programs that use asio need to have at least one I/O execution context, 46 such as an <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> 47 or <a class="link" href="../reference/thread_pool.html" title="thread_pool">thread_pool</a> object. 48 An I/O execution context provides access to I/O functionality. We declare 49 an object of type <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> 50 first thing in the main function. 51 </p> 52<pre class="programlisting">int main() 53{ 54 boost::asio::io_context io; 55</pre> 56<p> 57 Next we declare an object of type boost::asio::steady_timer. The core asio 58 classes that provide I/O functionality (or as in this case timer functionality) 59 always take a reference to an io_context as their first constructor argument. 60 The second argument to the constructor sets the timer to expire 5 seconds 61 from now. 62 </p> 63<pre class="programlisting"> boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); 64</pre> 65<p> 66 In this simple example we perform a blocking wait on the timer. That is, 67 the call to <a class="link" href="../reference/basic_waitable_timer/wait.html" title="basic_waitable_timer::wait">steady_timer::wait()</a> 68 will not return until the timer has expired, 5 seconds after it was created 69 (i.e. not from when the wait starts). 70 </p> 71<p> 72 A timer is always in one of two states: "expired" or "not 73 expired". If the <a class="link" href="../reference/basic_waitable_timer/wait.html" title="basic_waitable_timer::wait">steady_timer::wait()</a> 74 function is called on an expired timer, it will return immediately. 75 </p> 76<pre class="programlisting"> t.wait(); 77</pre> 78<p> 79 Finally we print the obligatory <code class="computeroutput">"Hello, world!"</code> message 80 to show when the timer has expired. 81 </p> 82<pre class="programlisting"> std::cout << "Hello, world!" << std::endl; 83 84 return 0; 85} 86</pre> 87<p> 88 See the <a class="link" href="tuttimer1/src.html" title="Source listing for Timer.1">full source listing</a> 89 </p> 90<p> 91 Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> 92 </p> 93<p> 94 Next: <a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - Using a timer 95 asynchronously</a> 96 </p> 97</div> 98<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 99<td align="left"></td> 100<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 101 Kohlhoff<p> 102 Distributed under the Boost Software License, Version 1.0. (See accompanying 103 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>) 104 </p> 105</div></td> 106</tr></table> 107<hr> 108<div class="spirit-nav"> 109<a accesskey="p" href="../tutorial.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tuttimer1/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 110</div> 111</body> 112</html> 113