1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Source listing for Timer.4</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="../tuttimer4.html" title="Timer.4 - Using a member function as a handler"> 9<link rel="prev" href="../tuttimer4.html" title="Timer.4 - Using a member function as a handler"> 10<link rel="next" href="../tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs"> 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="../tuttimer4.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tuttimer4.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="../tuttimer5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.tutorial.tuttimer4.src"></a><a class="link" href="src.html" title="Source listing for Timer.4">Source listing for 28 Timer.4</a> 29</h4></div></div></div> 30<pre class="programlisting">// 31// timer.cpp 32// ~~~~~~~~~ 33// 34// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) 35// 36// Distributed under the Boost Software License, Version 1.0. (See accompanying 37// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 38// 39 40#include <iostream> 41#include <boost/asio.hpp> 42#include <boost/bind/bind.hpp> 43 44class printer 45{ 46public: 47 printer(boost::asio::io_context& io) 48 : timer_(io, boost::asio::chrono::seconds(1)), 49 count_(0) 50 { 51 timer_.async_wait(boost::bind(&printer::print, this)); 52 } 53 54 ~printer() 55 { 56 std::cout << "Final count is " << count_ << std::endl; 57 } 58 59 void print() 60 { 61 if (count_ < 5) 62 { 63 std::cout << count_ << std::endl; 64 ++count_; 65 66 timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1)); 67 timer_.async_wait(boost::bind(&printer::print, this)); 68 } 69 } 70 71private: 72 boost::asio::steady_timer timer_; 73 int count_; 74}; 75 76int main() 77{ 78 boost::asio::io_context io; 79 printer p(io); 80 io.run(); 81 82 return 0; 83} 84</pre> 85<p> 86 Return to <a class="link" href="../tuttimer4.html" title="Timer.4 - Using a member function as a handler">Timer.4 - Using 87 a member function as a handler</a> 88 </p> 89</div> 90<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 91<td align="left"></td> 92<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 93 Kohlhoff<p> 94 Distributed under the Boost Software License, Version 1.0. (See accompanying 95 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>) 96 </p> 97</div></td> 98</tr></table> 99<hr> 100<div class="spirit-nav"> 101<a accesskey="p" href="../tuttimer4.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tuttimer4.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="../tuttimer5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 102</div> 103</body> 104</html> 105