Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 3ff61ef50f7b6da9fdee54352ae073f9 > files > 2781

asio-1.4.8-7.mga5.i586.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Timer.1 - Using a timer synchronously</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
<link rel="start" href="../../index.html" title="Asio">
<link rel="up" href="../tutorial.html" title="Tutorial">
<link rel="prev" href="../tutorial.html" title="Tutorial">
<link rel="next" href="tuttimer1/src.html" title="Source listing for Timer.1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="tuttimer1/src.html"><img src="../../next.png" alt="Next"></a>
</div>
<div class="section" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="asio.tutorial.tuttimer1"></a><a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - Using a timer synchronously</a>
</h3></div></div></div>
<p>
        This tutorial program introduces asio by showing how to perform a blocking
        wait on a timer.
      </p>
<p>
        We start by including the necessary header files.
      </p>
<p>
        All of the asio classes can be used by simply including the <code class="computeroutput"><span class="string">"asio.hpp"</span></code> header file.
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">asio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        Since this example uses timers, we need to include the appropriate Boost.Date_Time
        header file for manipulating times.
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">date_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        All programs that use asio need to have at least one <a class="link" href="../reference/io_service.html" title="io_service">io_service</a>
        object. This class provides access to I/O functionality. We declare an object
        of this type first thing in the main function.
      </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="identifier">io</span><span class="special">;</span>
</pre>
<p>
        Next we declare an object of type asio::deadline_timer. The core asio classes
        that provide I/O functionality (or as in this case timer functionality) always
        take a reference to an io_service as their first constructor argument. The
        second argument to the constructor sets the timer to expire 5 seconds from
        now.
      </p>
<pre class="programlisting">  <span class="identifier">asio</span><span class="special">::</span><span class="identifier">deadline_timer</span> <span class="identifier">t</span><span class="special">(</span><span class="identifier">io</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">5</span><span class="special">));</span>
</pre>
<p>
        In this simple example we perform a blocking wait on the timer. That is,
        the call to <a class="link" href="../reference/basic_deadline_timer/wait.html" title="basic_deadline_timer::wait">deadline_timer::wait()</a>
        will not return until the timer has expired, 5 seconds after it was created
        (i.e. not from when the wait starts).
      </p>
<p>
        A deadline timer is always in one of two states: "expired" or "not
        expired". If the <a class="link" href="../reference/basic_deadline_timer/wait.html" title="basic_deadline_timer::wait">deadline_timer::wait()</a>
        function is called on an expired timer, it will return immediately.
      </p>
<pre class="programlisting">  <span class="identifier">t</span><span class="special">.</span><span class="identifier">wait</span><span class="special">();</span>
</pre>
<p>
        Finally we print the obligatory <code class="computeroutput"><span class="string">"Hello,
        world!"</span></code> message to show when the timer has expired.
      </p>
<pre class="programlisting">  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello, world!\n"</span><span class="special">;</span>

  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        See the <a class="link" href="tuttimer1/src.html" title="Source listing for Timer.1">full source listing</a>
      </p>
<p>
        Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a>
      </p>
<p>
        Next: <a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - Using a timer asynchronously</a>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2003 - 2010 Christopher M. Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="tuttimer1/src.html"><img src="../../next.png" alt="Next"></a>
</div>
</body>
</html>