Revision: 290 Author: totti19851101 Date: Mon Aug 24 19:45:12 2009 Log: [No log message] http://code.google.com/p/boost-doc-zh/source/detail?r=290 Modified: /trunk/doc/html/boost_asio/tutorial/tuttimer2.html =======================================--- /trunk/doc/html/boost_asio/tutorial/tuttimer2.html Wed May 27 03:09:50 2009 +++ /trunk/doc/html/boost_asio/tutorial/tuttimer2.html Mon Aug 24 19:45:12 2009
@@ -1,6 +1,6 @@ <html> <head> -<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Timer.2 - Using a timer asynchronously</title> <link rel="stylesheet" href="../../boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.74.3"> @@ -24,23 +24,18 @@ </div> <div class="section" lang="en"> <div class="titlepage"><div><div><h3 class="title">-<a name="boost_asio.tutorial.tuttimer2"></a><a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously"> Timer.2 - Using a timer
- asynchronously</a>+<a name="boost_asio.tutorial.tuttimer2"></a><a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously"> Timer.2 - 使用异步定时器</a>
</h3></div></div></div> <p>- This tutorial program demonstrates how to use asio's asynchronous callback - functionality by modifying the program from tutorial Timer.1 to perform an
- asynchronous wait on the timer.+ 这个示例程序示范了如何通过修改Timer.1 中的程序,使用Asio的异步回调 功能在定时器中演示一个异步等待。
</p><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">asio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> <span class="preprocessor">#include</span> <span class="special"><</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">></span>
</pre> <p>- Using asio's asynchronous functionality means having a callback function - that will be called when an asynchronous operation completes. In this program - we define a function called <code class="computeroutput"><span class="identifier">print</span></code>
- to be called when the asynchronous wait finishes.+ 使用Asio的异步功能意味着当一个异步操作完成时一个回调函数将被调用。 在本程序中我们定义一个名为<code class="computeroutput"><span class="identifier">print</span></code>
+ 的函数,在异步等待结束后这个函数将被调用。 </p><pre class="programlisting"><span class="keyword">void</span> <span class="identifier">print</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&</span> <span class="comment">/*e*/</span><span class="special">)</span>
<span class="special">{</span> @@ -54,38 +49,29 @@<span class="identifier">boost</span><span class="special">::</span><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>- Next, instead of doing a blocking wait as in tutorial Timer.1, we call the
+ 接下来,我们调用<a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a> - function to perform an asynchronous wait. When calling this function we pass - the <code class="computeroutput"><span class="identifier">print</span></code> callback handler
- that was defined above.+ 函数执行一个异步等待去取代Timer.1例中的阻塞等待。当调用这个函数时我 们传入上面定义的<code class="computeroutput"><span class="identifier">print</span></code>回调句柄。
</p><pre class="programlisting"> <span class="identifier">t</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">print</span><span class="special">);</span>
</pre> <p>- Finally, we must call the <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>
- member function on the io_service object.+ 最后,我们必须在io_service对象上调用<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>
+ 成员函数。 </p> <p>- The asio library provides a guarantee that callback handlers will only be - called from threads that are currently calling <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>. - Therefore unless the <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> - function is called the callback for the asynchronous wait completion will
- never be invoked.+ Asio保证回调句柄仅仅能被<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>启动的当前线程所调用。 + 因此,如果<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> + 函数不执行,用于异步等待完成时的回调函数(在本例中为print函数)将永 远不会被调用。
</p> <p>- The <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>
- function will also continue to run while there is still "work"- to do. In this example, the work is the asynchronous wait on the timer, so - the call will not return until the timer has expired and the callback has
- completed.+ 当仍旧有“工作”可做时,<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> + 函数会继续运行。在本例中,“工作”是定时器的异步等待,因此,直到定时 器终止和回调函数执行完成,程序才会返回。
</p> <p>- It is important to remember to give the io_service some work to do before - calling <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>. - For example, if we had omitted the above call to <a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a>, - the io_service would not have had any work to do, and consequently <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> would
- have returned immediately.+ 在调用<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a>之前确保给 io_service 一些工作 去做,这非常重要。 + 例如,如果我们省略了上面调用的<a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a> + 函数,io_service对象将没有任何事情去做,因此<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> 将立即返回。
</p><pre class="programlisting"> <span class="identifier">io</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
@@ -93,18 +79,16 @@ <span class="special">}</span> </pre> <p>- See the <a class="link" href="tuttimer2/src.html" title="Source listing for Timer.2">full source listing</a> + 查看<a class="link" href="tuttimer2/src.html" title="Source listing for Timer.2">本例全部源代码</a>
</p> <p>- Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> + 返回<a class="link" href="../tutorial.html" title="Tutorial">指南 </a>
</p> <p>- Previous: <a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - Using a
- timer synchronously</a>+ 前一例: <a class="link" href="tuttimer1.html" title="Timer.1 - Using a timer synchronously">Timer.1 - 使用同步定时器</a>
</p> <p>- Next: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - Binding arguments
- to a handler</a>+ 后一例: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - 回调函数绑定参数</a>
</p> </div><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"; width="100%"><tr>