1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Timer.2 - Using a timer asynchronously</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="tuttimer1/src.html" title="Source listing for Timer.1"> 10<link rel="next" href="tuttimer2/src.html" title="Source listing for Timer.2"> 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="tuttimer1/src.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="tuttimer2/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.tuttimer2"></a><a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - Using a timer 28 asynchronously</a> 29</h3></div></div></div> 30<p> 31 This tutorial program demonstrates how to use asio's asynchronous callback 32 functionality by modifying the program from tutorial Timer.1 to perform an 33 asynchronous wait on the timer. 34 </p> 35<pre class="programlisting">#include <iostream> 36#include <boost/asio.hpp> 37</pre> 38<p> 39 Using asio's asynchronous functionality means having a callback function 40 that will be called when an asynchronous operation completes. In this program 41 we define a function called <code class="computeroutput">print</code> to be called when the asynchronous 42 wait finishes. 43 </p> 44<pre class="programlisting">void print(const boost::system::error_code& /*e*/) 45{ 46 std::cout << "Hello, world!" << std::endl; 47} 48 49int main() 50{ 51 boost::asio::io_context io; 52 53 boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); 54</pre> 55<p> 56 Next, instead of doing a blocking wait as in tutorial Timer.1, we call the 57 <a class="link" href="../reference/basic_waitable_timer/async_wait.html" title="basic_waitable_timer::async_wait">steady_timer::async_wait()</a> 58 function to perform an asynchronous wait. When calling this function we pass 59 the <code class="computeroutput">print</code> callback handler that was defined above. 60 </p> 61<pre class="programlisting"> t.async_wait(&print); 62</pre> 63<p> 64 Finally, we must call the <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a> 65 member function on the io_context object. 66 </p> 67<p> 68 The asio library provides a guarantee that callback handlers will only be 69 called from threads that are currently calling <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a>. 70 Therefore unless the <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a> 71 function is called the callback for the asynchronous wait completion will 72 never be invoked. 73 </p> 74<p> 75 The <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a> 76 function will also continue to run while there is still "work" 77 to do. In this example, the work is the asynchronous wait on the timer, so 78 the call will not return until the timer has expired and the callback has 79 completed. 80 </p> 81<p> 82 It is important to remember to give the io_context some work to do before 83 calling <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a>. 84 For example, if we had omitted the above call to <a class="link" href="../reference/basic_waitable_timer/async_wait.html" title="basic_waitable_timer::async_wait">steady_timer::async_wait()</a>, 85 the io_context would not have had any work to do, and consequently <a class="link" href="../reference/io_context/run.html" title="io_context::run">io_context::run()</a> would 86 have returned immediately. 87 </p> 88<pre class="programlisting"> io.run(); 89 90 return 0; 91} 92</pre> 93<p> 94 See the <a class="link" href="tuttimer2/src.html" title="Source listing for Timer.2">full source listing</a> 95 </p> 96<p> 97 Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> 98 </p> 99<p> 100 Previous: <a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - Using a 101 timer synchronously</a> 102 </p> 103<p> 104 Next: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - Binding arguments 105 to a handler</a> 106 </p> 107</div> 108<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 109<td align="left"></td> 110<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 111 Kohlhoff<p> 112 Distributed under the Boost Software License, Version 1.0. (See accompanying 113 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>) 114 </p> 115</div></td> 116</tr></table> 117<hr> 118<div class="spirit-nav"> 119<a accesskey="p" href="tuttimer1/src.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="tuttimer2/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 120</div> 121</body> 122</html> 123