~ubuntu-branches/ubuntu/intrepid/asio/intrepid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<HTML>
  <HEAD>
    <TITLE>asio Tutorial: Source listing for Timer.5</TITLE>
    <LINK HREF="asio.css" REL="stylesheet" TYPE="text/css">
    <LINK HREF="tabs.css" REL="stylesheet" TYPE="text/css">
  </HEAD>
  <BODY BGCOLOR="#FFFFFF">
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
          <TD ALIGN="LEFT">
            <B>asio 0.3.8rc3</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Home</A> |
            <A CLASS="qindex" HREF="../reference/index.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/index.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/index.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/index.html">Design</A>
          </TD>
        </TR>
      </TABLE>
    </DIV>
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
          <TD ALIGN="LEFT">
            <B>Tutorial</B>
          </TD>
        </TR>
      </TABLE>
    </DIV>
<!-- Generated by Doxygen 1.5.1 -->
<h1><a class="anchor" name="tuttimer5src">Source listing for Timer.5</a></h1><div class="fragment"><pre class="fragment"><span class="comment">//</span>
<span class="comment">// timer.cpp</span>
<span class="comment">// ~~~~~~~~~</span>
<span class="comment">//</span>
<span class="comment">// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)</span>
<span class="comment">//</span>
<span class="comment">// Distributed under the Boost Software License, Version 1.0. (See accompanying</span>
<span class="comment">// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</span>
<span class="comment">//</span>

<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;asio.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/bind.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;</span>

<span class="keyword">class </span>printer
{
<span class="keyword">public</span>:
  printer(<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a>&amp; io)
    : strand_(io),
      timer1_(io, boost::posix_time::seconds(1)),
      timer2_(io, boost::posix_time::seconds(1)),
      count_(0)
  {
    timer1_.async_wait(strand_.wrap(boost::bind(&amp;printer::print1, <span class="keyword">this</span>)));
    timer2_.async_wait(strand_.wrap(boost::bind(&amp;printer::print2, <span class="keyword">this</span>)));
  }

  ~printer()
  {
    std::cout &lt;&lt; <span class="stringliteral">"Final count is "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
  }

  <span class="keywordtype">void</span> print1()
  {
    <span class="keywordflow">if</span> (count_ &lt; 10)
    {
      std::cout &lt;&lt; <span class="stringliteral">"Timer 1: "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
      ++count_;

      timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
      timer1_.async_wait(strand_.wrap(boost::bind(&amp;printer::print1, <span class="keyword">this</span>)));
    }
  }

  <span class="keywordtype">void</span> print2()
  {
    <span class="keywordflow">if</span> (count_ &lt; 10)
    {
      std::cout &lt;&lt; <span class="stringliteral">"Timer 2: "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
      ++count_;

      timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
      timer2_.async_wait(strand_.wrap(boost::bind(&amp;printer::print2, <span class="keyword">this</span>)));
    }
  }

<span class="keyword">private</span>:
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00035.html">asio::strand</a> strand_;
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00006.html">asio::deadline_timer</a> timer1_;
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00006.html">asio::deadline_timer</a> timer2_;
  <span class="keywordtype">int</span> count_;
};

<span class="keywordtype">int</span> main()
{
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a> io;
  printer p(io);
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00052.html">asio::thread</a> t(boost::bind(&amp;<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html#c84bed0d1dd061bc71010ba1228439da">asio::io_service::run</a>, &amp;io));
  io.<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html#c84bed0d1dd061bc71010ba1228439da">run</a>();
  t.join();

  <span class="keywordflow">return</span> 0;
}
</pre></div> Return to <a class="el" href="tuttimer5.html">Timer.5 - Synchronising handlers in multithreaded programs</a>     <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
          <TD ALIGN="LEFT">
            <B>asio 0.3.8rc3</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Home</A> |
            <A CLASS="qindex" HREF="../reference/index.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/index.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/index.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/index.html">Design</A>
          </TD>
        </TR>
      </TABLE>
    </DIV>
  </BODY>
</HTML>