Asio 定时器
来源:互联网 发布:淘宝91邀请码多少钱 编辑:程序博客网 时间:2024/05/01 13:56
这几天看《Boost程序库完全开发指南》的第十二章有介绍Asio的使用方法,介绍很简单,只介绍了定时器、网络通信。下面对书中提到的定时器编程代码(
boost_async_timer_bind.cpp)如下:
/*author:AlexChendate:2014/04/22#g++ -g -o main boost_async_timer_bind.cpp -lboost_system*/#define BOOST_REGEX_NO_LIB#define BOOST_DATE_TIME_SOURCE#define BOOST_SYSTEM_NO_LIB#include <boost/asio.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/function.hpp>#include <boost/bind.hpp>#include <iostream>using namespace std;using namespace boost;using namespace boost::asio;void SyncTimer(){io_service ios;deadline_timer t(ios,posix_time::seconds(2));cout<<t.expires_at()<<""<<time(NULL)<<endl;t.wait();cout<<"Hello!SyncTimer...."<<""<<time(NULL)<<endl;}void Print(const boost::system::error_code&){cout<<"Hello!AsyncTimer...."<<""<<time(NULL)<<endl;}void AsyncTimer(){io_service ios;deadline_timer t(ios,posix_time::seconds(2));cout<<t.expires_at()<<""<<time(NULL)<<endl;t.async_wait(Print);cout<<"It show before t expired..."<<""<<time(NULL)<<endl;ios.run();}class a_timer{private:int count;int count_max;function<void()> f;deadline_timer t;public:template<typename F>a_timer(io_service& ios,int x,F func):f(func),count_max(x),count(0),t(ios,posix_time::millisec(500)){t.async_wait(boost::bind(&a_timer::call_func,this,placeholders::error));}void call_func(const boost::system::error_code& e){if(count >= count_max){return;}++count;f();t.expires_at(t.expires_at() + posix_time::millisec(500));t.async_wait(boost::bind(&a_timer::call_func,this,placeholders::error));}};void Print1(){cout<<"Hello,Print1..."<<endl;}void Print2(){cout<<"Hello,Print2..."<<endl;}void TestBind(){io_service ios;a_timer at1(ios,5,Print1);a_timer at2(ios,5,Print2);ios.run();}int main(){cout<<endl<<"Test SyncTimer ...."<<endl;SyncTimer();cout<<endl<<"Test AsyncTimer ...."<<endl;AsyncTimer();cout<<endl<<"Test TestBind ...."<<endl;TestBind();cout<<endl<<"successful!..."<<endl;}
0 0
- Asio 定时器
- boost::asio 定时器
- boost asio定时器学习
- boost asio 定时器 deadline_timer cancel
- boost库之asio库, 定时器
- boost中的asio实现定时器功能
- Boost Asio 1.56 学习之同步定时器
- Boost中ASIO的一些用法-定时器
- boost之asio实现定时器使用实例
- 【boost】boost::asio(1)--timer定时器
- 用 select 实现 定时器功能,摘抄自boost.asio
- 用asio的定时器实现带超时的connect,备忘
- LinuxC/C++编程基础(14) asio定时器的用法
- C++ Boost库 asio同步/异步模式[ 定时器来体现 ]
- boost::asio(1):封装了定时器类
- 【boost学习】之boost::asio(1)——Timer定时器
- boost::asio名字空间中的deadline_time类:定时器
- boost-asio学习1——定时器(同步、异步)
- 在PHP语言中使用JSON
- Java: java.text.Format体系总结
- NSCopying协议
- 小而美的LBS应用龙门阵——成都开发者沙龙
- .net平台借助第三方推送服务在推送Android消息(极光推送)
- Asio 定时器
- Cow Hurdles(floyd)
- makefile
- IO忽略ssl
- 产生不相同的随机数
- WEP、WPA和WPA2的区别
- 谈谈我认识的Aurigma Upload Suite
- CCLuaObjcBridge调Objective-C方法传索引数组报invalid key to 'next'错调试
- SDWebImageManagerDelegate 协议参考