This tutorial program introduces asio by showing how to perform a blocking wait on a timer.
这个示例程序通过展示在定时器中执行一个阻塞等待( blocking wait )来介绍Asio。
We start by including the necessary header files.
让我们从必须包含的头文件开始。
All of the asio classes can be used by simply including the "asio.hpp"
header file.
所有的Asio类只要简单的包含"asio.hpp"头文件便可使用。
#include <iostream>
#include <boost/asio.hpp>
Since this example users timers, we need to include the appropriate Boost.Date_Time header file for manipulating times.
因为本程序中使用了定时器,我们需要包含相应的的Boost.Date_Time 头文件来处理时间操作。
#include <boost/date_time/posix_time/posix_time.hpp>
All programs that use asio need to have at least one boost::asio::io_service object. This class provides access to I/O functionality. We declare an object of this type first thing in the main function.
使用Asio的所有程序都至少需要一个提供访问I/O功能的boost::asio::io_service对象。因此在主函数中我们做的第一件事就是声明一个这个类型的对象。
int main()
{
boost::asio::io_service io;
Next we declare an object of type boost::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.
接下来我们声明一个boost::asio::deadline_timer类型的对象。作为Asio的核心类,它提供的I/O功能(在此为定时器功能)通常用一个io_service 的引用作为其构造函数的第一个参数。第二个参数设置一个从现在开始5秒后终止的定时器。
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
In this simple example we perform a blocking wait on the timer. That is, the call to boost::asio::deadline_timer::wait() will not return until the timer has expired, 5 seconds after it was created (i.e. not from when the wait starts).
在这个简单的程序中,我们用定时器演示一个阻塞等待。boost::asio::deadline_timer::wait()函数调用直到定时器终止(从定时器被创建算起,五秒后终止)才会返回。
A deadline timer is always in one of two states: "expired" or "not expired". If the boost::asio::deadline_timer::wait() function is called on an expired timer, it will return immediately.
一个deadline timer 通常是下面两种状态中的一种:"expired(终止)" 或"not expired(不终止)"。如果boost::asio::deadline_timer::wait()函数被一个已经终止的定时器调用, 它将立即返回。
Finally we print the obligatory "Hello, world!"
message to show when the timer has expired.
最后我们打印出“Hello,world”信息以显示定时器已经终止。
std::cout << "Hello, world! ";
return 0;
}
See the full source listing
查看本例的全部源码:
//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world! ";
return 0;
}
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的异步回调功能在定时器中演示一个异步等待。
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
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 print
to be called when the asynchronous wait finishes.
使用Asio的异步功能意味着当一个异步操作完成时一个回调函数将被调用。在本程序中我们定义一个名为“print”的函数,在异步等待结束后这个函数将被调用。
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world! ";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
Next, instead of doing a blocking wait as in tutorial Timer.1, we call the boost::asio::deadline_timer::async_wait() function to perform an asynchronous wait. When calling this function we pass the print
callback handler that was defined above.
接下来,我们调用boost::asio::deadline_timer::async_wait() 函数执行一个异步等待去取代Timer.1例中的阻塞等待。当调用这个函数时我们传入上面定义的print回调句柄。
t.async_wait(print);
Finally, we must call the boost::asio::io_service::run() member function on the io_service object.
最后,我们必须在io_service对象上调用boost::asio::io_service::run() 成员函数。
The asio library provides a guarantee that callback handlers will only be called from threads that are currently calling boost::asio::io_service::run(). Therefore unless the boost::asio::io_service::run() function is called the callback for the asynchronous wait completion will never be invoked.
Asio保证回调句柄仅仅能被boost::asio::io_service::run()启动的当前线程所调用。因此,如果boost::asio::io_service::run() 函数不执行,用于异步等待完成时的回调函数(在本例中为print函数)将永远不会被调用。
The boost::asio::io_service::run() 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.
当仍旧有“工作”可做时,boost::asio::io_service::run() 函数会继续运行。在本例中,“工作”是定时器的异步等待,因此,直到定时器终止和回调函数执行完成,程序才会返回。
It is important to remember to give the io_service some work to do before calling boost::asio::io_service::run(). For example, if we had omitted the above call to boost::asio::deadline_timer::async_wait(), the io_service would not have had any work to do, and consequently boost::asio::io_service::run() would have returned immediately.
在调用boost::asio::io_service::run()之前确保给io_service 一些工作去做,这非常重要。例如,如果我们省略了上面调用的boost::asio::deadline_timer::async_wait()函数,io_service对象将没有任何事情去做,因此boost::asio::io_service::run() 将立即返回。
See the full source listing
查看本例的全部源码:
posted on 2008-04-20 01:17
王晓轩 阅读(7046)
评论(54) 编辑 收藏 引用 所属分类:
C\C++