This tutorial program introduces asio by showing how to perform a blocking wait on a timer.
這個(gè)示例程序通過(guò)展示在定時(shí)器中執(zhí)行一個(gè)阻塞等待( blocking wait )來(lái)介紹Asio。
We start by including the necessary header files.
讓我們從必須包含的頭文件開(kāi)始。
All of the asio classes can be used by simply including the "asio.hpp"
header file.
所有的Asio類只要簡(jiǎn)單的包含"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.
因?yàn)楸境绦蛑惺褂昧硕〞r(shí)器,我們需要包含相應(yīng)的的Boost.Date_Time 頭文件來(lái)處理時(shí)間操作。
#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的所有程序都至少需要一個(gè)提供訪問(wèn)I/O功能的boost::asio::io_service對(duì)象。因此在主函數(shù)中我們做的第一件事就是聲明一個(gè)這個(gè)類型的對(duì)象。
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.
接下來(lái)我們聲明一個(gè)boost::asio::deadline_timer類型的對(duì)象。作為Asio的核心類,它提供的I/O功能(在此為定時(shí)器功能)通常用一個(gè)io_service 的引用作為其構(gòu)造函數(shù)的第一個(gè)參數(shù)。第二個(gè)參數(shù)設(shè)置一個(gè)從現(xiàn)在開(kāi)始5秒后終止的定時(shí)器。
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).
在這個(gè)簡(jiǎn)單的程序中,我們用定時(shí)器演示一個(gè)阻塞等待。boost::asio::deadline_timer::wait()函數(shù)調(diào)用直到定時(shí)器終止(從定時(shí)器被創(chuàng)建算起,五秒后終止)才會(huì)返回。
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.
一個(gè)deadline timer 通常是下面兩種狀態(tài)中的一種:"expired(終止)" 或"not expired(不終止)"。如果boost::asio::deadline_timer::wait()函數(shù)被一個(gè)已經(jīng)終止的定時(shí)器調(diào)用, 它將立即返回。
Finally we print the obligatory "Hello, world!"
message to show when the timer has expired.
最后我們打印出“Hello,world”信息以顯示定時(shí)器已經(jīng)終止。
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.
這個(gè)示例程序示范了如何通過(guò)修改Timer.1 中的程序,使用Asio的異步回調(diào)功能在定時(shí)器中演示一個(gè)異步等待。
#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的異步功能意味著當(dāng)一個(gè)異步操作完成時(shí)一個(gè)回調(diào)函數(shù)將被調(diào)用。在本程序中我們定義一個(gè)名為“print”的函數(shù),在異步等待結(jié)束后這個(gè)函數(shù)將被調(diào)用。
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.
接下來(lái),我們調(diào)用boost::asio::deadline_timer::async_wait() 函數(shù)執(zhí)行一個(gè)異步等待去取代Timer.1例中的阻塞等待。當(dāng)調(diào)用這個(gè)函數(shù)時(shí)我們傳入上面定義的print回調(diào)句柄。
t.async_wait(print);
Finally, we must call the boost::asio::io_service::run() member function on the io_service object.
最后,我們必須在io_service對(duì)象上調(diào)用boost::asio::io_service::run() 成員函數(shù)。
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保證回調(diào)句柄僅僅能被boost::asio::io_service::run()啟動(dòng)的當(dāng)前線程所調(diào)用。因此,如果boost::asio::io_service::run() 函數(shù)不執(zhí)行,用于異步等待完成時(shí)的回調(diào)函數(shù)(在本例中為print函數(shù))將永遠(yuǎn)不會(huì)被調(diào)用。
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.
當(dāng)仍舊有“工作”可做時(shí),boost::asio::io_service::run() 函數(shù)會(huì)繼續(xù)運(yùn)行。在本例中,“工作”是定時(shí)器的異步等待,因此,直到定時(shí)器終止和回調(diào)函數(shù)執(zhí)行完成,程序才會(huì)返回。
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.
在調(diào)用boost::asio::io_service::run()之前確保給io_service 一些工作去做,這非常重要。例如,如果我們省略了上面調(diào)用的boost::asio::deadline_timer::async_wait()函數(shù),io_service對(duì)象將沒(méi)有任何事情去做,因此boost::asio::io_service::run() 將立即返回。
See the full source listing
查看本例的全部源碼:
posted on 2008-04-20 01:17
王曉軒 閱讀(7075)
評(píng)論(54) 編輯 收藏 引用 所屬分類:
C\C++