一、使用同步定时器
这个示例程序通过展示如何在一个定时器执行一个阻塞等待。
//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 <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& );
#endif
3、asiotest2.cpp
#include "asiotest2.h"
using namespace std;
using namespace boost;
void print(const boost::system::error_code& )
{
std::cout<<"hello,world!\n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
t.async_wait(&print);
io.run();
return 0;
}
三、绑定参数到处理程序
在本示例中,我们将在示例二修改程序,使定时器每秒被激活一次。这将显示如何传递额外的参数给你的处理函数。
1、makefile 文件同示例二makefile修改方法
2、头文件
#ifndef __ASIOTEST3__H__
#define __ASIOTEST3__H__
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#endif
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 main()
{
boost::asio::io_service io;
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));
io.run();
std::cout<<"Final count is" <<count<<"\n";
return 0;
}
四、使用成员函数做为处理程序示例
在本示例中,我们将看到如何使用一个类的成员函数作为回调处理程序。
1、makefile 同上面示例
2、头文件
#ifndef __ASIOTEST4__H__
#define __ASIOTEST4__H__
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
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"
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}
五、多线程的同步处理示例
本示例演示boost::asio::strand 在多线程程序中同步回调处理程
1、makefile同上
2、头文件
#ifndef __ASIOTEST5__H__
#define __ASIOTEST5__H__
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
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)));
}
~printer()
{
std::cout<<"Final count is " <<count_<<std::endl;
}
void print1()
{
if(count_ < 10)
{
std::cout<<"Timer 1: "<<count_<<std::endl;
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
}
}
void print2()
{
if(count_ < 10)
{
std::cout<<"Timer 2: " <<count_<<std::endl;
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
}
}
private:
boost::asio::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
};
#endif
3、CPP文件
#include "asiotest5.h"
int main()
{
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;
}