一、使用同步定時器
這個示例程式通過展示如何在一個定時器執行一個阻塞等待。
//makefile
#----------------------------------------------------------
#makefile helloworld測試用例
#
#-----------------------------------------------------------
ggg=g++
exe=asiotimer
#所有的.o檔案寫在這裡
obj = asiotimer.o
#所要關聯的cpp檔案寫在這裡
cpp = asiotimer.cpp
#加入庫檔案
libso = -lboost_thread -lboost_system
$(exe):$(obj)
@echo "連結開始................"
$(ggg) $(libso) -o $(exe) $(obj)
hw.o : $(cpp)
@echo "編譯開始................"
$(ggg) -std=c++11 -c $(cpp)
.PHONY : clean cleanall
cleanall:
@echo "開始make all..........."
-rm -rf $(exe) $(obj)
clean:
@echo "開始清理................"
-rm -rf $(obj)
2、asiotimer.h頭檔案
//asiotimer.h
#ifndef __ASIOTIMER__H__
#define __ASIOTIMER__H__
#include <iostream>
#include <boost/asio.hpp>
//#define BOOST_DATE_TIME_SOURCE
#include "boost/date_time/posix_time/posix_time.hpp"
#endif
3、asiotimer.cpp檔案
//asiotimer.cpp
#include "asiotimer.h"
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
t.wait();
std::cout<<"hello,world\n";
return 0;
}
二、使用異步定時器示例
本示例程式示範了如何使用Asio的異步回調功能由示例一修改程式 ,開啟計時器執行一個異步等待。
1、makefile檔案
makefile 與示例一基本相同,隻需要修改
exe=asiotest2
#所有的.o檔案寫在這裡
obj = asiotest2.o
#所要關聯的cpp檔案寫在這裡
cpp = asiotest2.cpp
2、asiotest2.h
#ifndef __ASIOTEST2__H__
#define __ASIOTEST2__H__
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& );
3、asiotest2.cpp
#include "asiotest2.h"
using namespace std;
using namespace boost;
void print(const boost::system::error_code& )
std::cout<<"hello,world!\n";
t.async_wait(&print);
io.run();
三、綁定參數到處理程式
在本示例中,我們将在示例二修改程式,使定時器每秒被激活一次。這将顯示如何傳遞額外的參數給你的處理函數。
1、makefile 檔案同示例二makefile修改方法
2、頭檔案
#ifndef __ASIOTEST3__H__
#define __ASIOTEST3__H__
#include <boost/bind.hpp>
3、CPP檔案
#include "asiotest3.h"
void print(const boost::system::error_code&,
boost::asio::deadline_timer* t,int* count)
if(*count<5)
{
std::cout<<*count<<"\n";
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(print,
boost::asio::placeholders::error,t,count));
}
int count=0;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));
t.async_wait(boost::bind(print,boost::asio::placeholders::error,
&t,&count));
std::cout<<"Final count is" <<count<<"\n";
四、使用成員函數做為處理程式示例
在本示例中,我們将看到如何使用一個類的成員函數作為回調處理程式。
1、makefile 同上面示例
#ifndef __ASIOTEST4__H__
#define __ASIOTEST4__H__
class printer
public:
printer(boost::asio::io_service& io)
:timer_(io,boost::posix_time::seconds(1)),
count_(0)
timer_.async_wait(boost::bind(&printer::print,this));
~printer()
std::cout<<"Final count is "<<count_<<"\n";
void print()
if(count_<5)
{
std::cout<<count_<<std::endl;
++count_;
timer_.expires_at(timer_.expires_at()+boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print,this));
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
3、cpp檔案
#include "asiotest4.h"
printer p(io);
五、多線程的同步處理示例
本示例示範boost::asio::strand 在多線程程式中同步回調處理程
1、makefile同上
#ifndef __ASIOTEST5__H__
#define __ASIOTEST5__H__
#include <boost/thread/thread.hpp>
printer(boost::asio::io_service& io):strand_(io),
timer1_(io,boost::posix_time::seconds(1)),
timer2_(io,boost::posix_time::seconds(1)),count_(0)
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
std::cout<<"Final count is " <<count_<<std::endl;
void print1()
if(count_ < 10)
std::cout<<"Timer 1: "<<count_<<std::endl;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
void print2()
std::cout<<"Timer 2: " <<count_<<std::endl;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
boost::asio::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
#endif
#include "asiotest5.h"
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run,&io));
io.run();
t.join();
return 0;
}
from:
<a href="http://blog.csdn.net/leitianjun/article/details/25740633">http://blog.csdn.net/leitianjun/article/details/25740633</a>