1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Source listing for Timer.5</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="../tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs"> 9<link rel="prev" href="../tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs"> 10<link rel="next" href="../tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client"> 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="../tuttimer5.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tuttimer5.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="../tutdaytime1.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.tuttimer5.src"></a><a class="link" href="src.html" title="Source listing for Timer.5">Source listing for 28 Timer.5</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/thread/thread.hpp> 43#include <boost/bind/bind.hpp> 44 45class printer 46{ 47public: 48 printer(boost::asio::io_context& io) 49 : strand_(boost::asio::make_strand(io)), 50 timer1_(io, boost::asio::chrono::seconds(1)), 51 timer2_(io, boost::asio::chrono::seconds(1)), 52 count_(0) 53 { 54 timer1_.async_wait(boost::asio::bind_executor(strand_, 55 boost::bind(&printer::print1, this))); 56 57 timer2_.async_wait(boost::asio::bind_executor(strand_, 58 boost::bind(&printer::print2, this))); 59 } 60 61 ~printer() 62 { 63 std::cout << "Final count is " << count_ << std::endl; 64 } 65 66 void print1() 67 { 68 if (count_ < 10) 69 { 70 std::cout << "Timer 1: " << count_ << std::endl; 71 ++count_; 72 73 timer1_.expires_at(timer1_.expiry() + boost::asio::chrono::seconds(1)); 74 75 timer1_.async_wait(boost::asio::bind_executor(strand_, 76 boost::bind(&printer::print1, this))); 77 } 78 } 79 80 void print2() 81 { 82 if (count_ < 10) 83 { 84 std::cout << "Timer 2: " << count_ << std::endl; 85 ++count_; 86 87 timer2_.expires_at(timer2_.expiry() + boost::asio::chrono::seconds(1)); 88 89 timer2_.async_wait(boost::asio::bind_executor(strand_, 90 boost::bind(&printer::print2, this))); 91 } 92 } 93 94private: 95 boost::asio::strand<boost::asio::io_context::executor_type> strand_; 96 boost::asio::steady_timer timer1_; 97 boost::asio::steady_timer timer2_; 98 int count_; 99}; 100 101int main() 102{ 103 boost::asio::io_context io; 104 printer p(io); 105 boost::thread t(boost::bind(&boost::asio::io_context::run, &io)); 106 io.run(); 107 t.join(); 108 109 return 0; 110} 111</pre> 112<p> 113 Return to <a class="link" href="../tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs">Timer.5 - Synchronising 114 handlers in multithreaded programs</a> 115 </p> 116</div> 117<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 118<td align="left"></td> 119<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 120 Kohlhoff<p> 121 Distributed under the Boost Software License, Version 1.0. (See accompanying 122 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>) 123 </p> 124</div></td> 125</tr></table> 126<hr> 127<div class="spirit-nav"> 128<a accesskey="p" href="../tuttimer5.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tuttimer5.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="../tutdaytime1.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 129</div> 130</body> 131</html> 132