3
<TITLE>asio Tutorial: Source listing for Timer.5</TITLE>
4
<LINK HREF="asio.css" REL="stylesheet" TYPE="text/css">
5
<LINK HREF="tabs.css" REL="stylesheet" TYPE="text/css">
7
<BODY BGCOLOR="#FFFFFF">
9
<TABLE BORDER="0" WIDTH="100%">
15
<A CLASS="qindex" HREF="../index.html">Home</A> |
16
<A CLASS="qindex" HREF="../reference/index.html">Reference</A> |
17
<A CLASS="qindex" HREF="../tutorial/index.html">Tutorial</A> |
18
<A CLASS="qindex" HREF="../examples/index.html">Examples</A> |
19
<A CLASS="qindex" HREF="../design/index.html">Design</A>
25
<TABLE BORDER="0" WIDTH="100%">
33
<!-- Generated by Doxygen 1.5.1 -->
34
<h1><a class="anchor" name="tuttimer5src">Source listing for Timer.5</a></h1><div class="fragment"><pre class="fragment"><span class="comment">//</span>
35
<span class="comment">// timer.cpp</span>
36
<span class="comment">// ~~~~~~~~~</span>
37
<span class="comment">//</span>
38
<span class="comment">// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)</span>
39
<span class="comment">//</span>
40
<span class="comment">// Distributed under the Boost Software License, Version 1.0. (See accompanying</span>
41
<span class="comment">// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</span>
42
<span class="comment">//</span>
44
<span class="preprocessor">#include <iostream></span>
45
<span class="preprocessor">#include <asio.hpp></span>
46
<span class="preprocessor">#include <boost/bind.hpp></span>
47
<span class="preprocessor">#include <boost/date_time/posix_time/posix_time.hpp></span>
49
<span class="keyword">class </span>printer
51
<span class="keyword">public</span>:
52
printer(<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a>& io)
54
timer1_(io, boost::posix_time::seconds(1)),
55
timer2_(io, boost::posix_time::seconds(1)),
58
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, <span class="keyword">this</span>)));
59
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, <span class="keyword">this</span>)));
64
std::cout << <span class="stringliteral">"Final count is "</span> << count_ << <span class="stringliteral">"\n"</span>;
67
<span class="keywordtype">void</span> print1()
69
<span class="keywordflow">if</span> (count_ < 10)
71
std::cout << <span class="stringliteral">"Timer 1: "</span> << count_ << <span class="stringliteral">"\n"</span>;
74
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
75
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, <span class="keyword">this</span>)));
79
<span class="keywordtype">void</span> print2()
81
<span class="keywordflow">if</span> (count_ < 10)
83
std::cout << <span class="stringliteral">"Timer 2: "</span> << count_ << <span class="stringliteral">"\n"</span>;
86
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
87
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, <span class="keyword">this</span>)));
91
<span class="keyword">private</span>:
92
<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00035.html">asio::strand</a> strand_;
93
<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00006.html">asio::deadline_timer</a> timer1_;
94
<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00006.html">asio::deadline_timer</a> timer2_;
95
<span class="keywordtype">int</span> count_;
98
<span class="keywordtype">int</span> main()
100
<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a> io;
102
<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00052.html">asio::thread</a> t(boost::bind(&<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html#c84bed0d1dd061bc71010ba1228439da">asio::io_service::run</a>, &io));
103
io.<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html#c84bed0d1dd061bc71010ba1228439da">run</a>();
106
<span class="keywordflow">return</span> 0;
108
</pre></div> Return to <a class="el" href="tuttimer5.html">Timer.5 - Synchronising handlers in multithreaded programs</a> <DIV CLASS="qindex">
109
<TABLE BORDER="0" WIDTH="100%">
115
<A CLASS="qindex" HREF="../index.html">Home</A> |
116
<A CLASS="qindex" HREF="../reference/index.html">Reference</A> |
117
<A CLASS="qindex" HREF="../tutorial/index.html">Tutorial</A> |
118
<A CLASS="qindex" HREF="../examples/index.html">Examples</A> |
119
<A CLASS="qindex" HREF="../design/index.html">Design</A>