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

« back to all changes in this revision

Viewing changes to doc/tutorial/tuttimer5src.html

  • Committer: Bazaar Package Importer
  • Author(s): Simon Richter
  • Date: 2007-09-07 11:10:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070907111041-f0uwhs0llvzj9ah5
Tags: upstream-0.3.8~rc3
ImportĀ upstreamĀ versionĀ 0.3.8~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<HTML>
 
2
  <HEAD>
 
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">
 
6
  </HEAD>
 
7
  <BODY BGCOLOR="#FFFFFF">
 
8
    <DIV CLASS="qindex">
 
9
      <TABLE BORDER="0" WIDTH="100%">
 
10
        <TR>
 
11
          <TD ALIGN="LEFT">
 
12
            <B>asio 0.3.8rc3</B>
 
13
          </TD>
 
14
          <TD ALIGN="RIGHT">
 
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>
 
20
          </TD>
 
21
        </TR>
 
22
      </TABLE>
 
23
    </DIV>
 
24
    <DIV CLASS="qindex">
 
25
      <TABLE BORDER="0" WIDTH="100%">
 
26
        <TR>
 
27
          <TD ALIGN="LEFT">
 
28
            <B>Tutorial</B>
 
29
          </TD>
 
30
        </TR>
 
31
      </TABLE>
 
32
    </DIV>
 
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>
 
43
 
 
44
<span class="preprocessor">#include &lt;iostream&gt;</span>
 
45
<span class="preprocessor">#include &lt;asio.hpp&gt;</span>
 
46
<span class="preprocessor">#include &lt;boost/bind.hpp&gt;</span>
 
47
<span class="preprocessor">#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;</span>
 
48
 
 
49
<span class="keyword">class </span>printer
 
50
{
 
51
<span class="keyword">public</span>:
 
52
  printer(<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a>&amp; io)
 
53
    : strand_(io),
 
54
      timer1_(io, boost::posix_time::seconds(1)),
 
55
      timer2_(io, boost::posix_time::seconds(1)),
 
56
      count_(0)
 
57
  {
 
58
    timer1_.async_wait(strand_.wrap(boost::bind(&amp;printer::print1, <span class="keyword">this</span>)));
 
59
    timer2_.async_wait(strand_.wrap(boost::bind(&amp;printer::print2, <span class="keyword">this</span>)));
 
60
  }
 
61
 
 
62
  ~printer()
 
63
  {
 
64
    std::cout &lt;&lt; <span class="stringliteral">"Final count is "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
 
65
  }
 
66
 
 
67
  <span class="keywordtype">void</span> print1()
 
68
  {
 
69
    <span class="keywordflow">if</span> (count_ &lt; 10)
 
70
    {
 
71
      std::cout &lt;&lt; <span class="stringliteral">"Timer 1: "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
 
72
      ++count_;
 
73
 
 
74
      timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
 
75
      timer1_.async_wait(strand_.wrap(boost::bind(&amp;printer::print1, <span class="keyword">this</span>)));
 
76
    }
 
77
  }
 
78
 
 
79
  <span class="keywordtype">void</span> print2()
 
80
  {
 
81
    <span class="keywordflow">if</span> (count_ &lt; 10)
 
82
    {
 
83
      std::cout &lt;&lt; <span class="stringliteral">"Timer 2: "</span> &lt;&lt; count_ &lt;&lt; <span class="stringliteral">"\n"</span>;
 
84
      ++count_;
 
85
 
 
86
      timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
 
87
      timer2_.async_wait(strand_.wrap(boost::bind(&amp;printer::print2, <span class="keyword">this</span>)));
 
88
    }
 
89
  }
 
90
 
 
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_;
 
96
};
 
97
 
 
98
<span class="keywordtype">int</span> main()
 
99
{
 
100
  <a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html">asio::io_service</a> io;
 
101
  printer p(io);
 
102
  <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));
 
103
  io.<a class="codeRef" doxygen="asio.doxytags:../reference/" href="../reference/a00032.html#c84bed0d1dd061bc71010ba1228439da">run</a>();
 
104
  t.join();
 
105
 
 
106
  <span class="keywordflow">return</span> 0;
 
107
}
 
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%">
 
110
        <TR>
 
111
          <TD ALIGN="LEFT">
 
112
            <B>asio 0.3.8rc3</B>
 
113
          </TD>
 
114
          <TD ALIGN="RIGHT">
 
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>
 
120
          </TD>
 
121
        </TR>
 
122
      </TABLE>
 
123
    </DIV>
 
124
  </BODY>
 
125
</HTML>