This tutorial program introduces asio by showing how to perform a blocking wait on a timer.
這個示例程序通過展示在定時器中執(zhí)行一個阻塞等待( 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.
因為本程序中使用了定時器,我們需要包含相應(yīng)的的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對象。因此在主函數(shù)中我們做的第一件事就是聲明一個這個類型的對象。
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 的引用作為其構(gòu)造函數(shù)的第一個參數(shù)。第二個參數(shù)設(shè)置一個從現(xiàn)在開始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()函數(shù)調(diào)用直到定時器終止(從定時器被創(chuàng)建算起,五秒后終止)才會返回。
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 通常是下面兩種狀態(tài)中的一種:"expired(終止)" 或"not expired(不終止)"。如果boost::asio::deadline_timer::wait()函數(shù)被一個已經(jīng)終止的定時器調(diào)用, 它將立即返回。
Finally we print the obligatory "Hello, world!"
message to show when the timer has expired.
最后我們打印出“Hello,world”信息以顯示定時器已經(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.
這個示例程序示范了如何通過修改Timer.1 中的程序,使用Asio的異步回調(diào)功能在定時器中演示一個異步等待。
#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的異步功能意味著當一個異步操作完成時一個回調(diào)函數(shù)將被調(diào)用。在本程序中我們定義一個名為“print”的函數(shù),在異步等待結(jié)束后這個函數(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.
接下來,我們調(diào)用boost::asio::deadline_timer::async_wait() 函數(shù)執(zhí)行一個異步等待去取代Timer.1例中的阻塞等待。當調(diào)用這個函數(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對象上調(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()啟動的當前線程所調(diào)用。因此,如果boost::asio::io_service::run() 函數(shù)不執(zhí)行,用于異步等待完成時的回調(diào)函數(shù)(在本例中為print函數(shù))將永遠不會被調(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.
當仍舊有“工作”可做時,boost::asio::io_service::run() 函數(shù)會繼續(xù)運行。在本例中,“工作”是定時器的異步等待,因此,直到定時器終止和回調(diào)函數(shù)執(zhí)行完成,程序才會返回。
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對象將沒有任何事情去做,因此boost::asio::io_service::run() 將立即返回。
See the full source listing
查看本例的全部源碼:
posted on 2008-04-20 01:17
王曉軒 閱讀(7100)
評論(54) 編輯 收藏 引用 所屬分類:
C\C++