【08】 Boost库学习笔记之定时器(Timer)
2015-08-22 10:48
357 查看
一 示例代码1 同步定时器
#include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_service io; boost::asio::deadline_timer timer(io, boost::posix_time::seconds(10)); //boost::asio::deadline_timer timer(io, boost::posix_time::milliseconds(10)); //boost::asio::deadline_timer timer(io, boost::posix_time::microseconds(10)); timer.wait(); std::cout << "Hello, world!\n"; return 0; }2 异步定时器
#include <iostream> #include <boost/asio.hpp> void timer_handler(const boost::system::error_code& /*e*/) { std::cout << "Hello, world!\n"; } int main() { boost::asio::io_service io; boost::asio::deadline_timer timer(io, boost::posix_time::seconds(5)); timer.async_wait(&timer_handler); io.run(); return 0; }3 超时重传
#include <boost/bind.hpp> #include <boost/asio.hpp> #include <iostream> void timer_handler(boost::asio::deadline_timer* timer , const boost::system::error_code& err) { if (err) { std::cout << "Timer cancel" << std::endl; } std::cout<< "hello Timer"<<std::endl; } void timer_enable(boost::asio::deadline_timer* timer, size_t seconds_from_now) { assert(timer); timer->expires_from_now(boost::posix_time::seconds(seconds_from_now)); timer->async_wait(boost::bind(timer_handler, timer, boost::asio::placeholders::error)); } void main() { boost::asio::io_service ios; boost::asio::deadline_timer t(ios); timer_enable(&t,5); boost::asio::deadline_timer timer2(ios, boost::posix_time::seconds(2)); timer2.wait(); t.cancel(); ios.run(); return ; }4 定时任务
#include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> void print(const boost::system::error_code& e, boost::asio::deadline_timer* t) { std::cout<<"timer task"<<std::endl; t->expires_at(t->expires_at()+ boost::posix_time::seconds(1)); t->async_wait(boost::bind(print,boost::asio::placeholders::error,t)); } void timertask() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(1)); t.async_wait(boost::bind(print,boost::asio::placeholders::error,&t)); io.run(); } int main() { timertask(); return 0; }二源码分析 http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/deadline_timer.html 1 在boost_1_59_0\boost\asio\deadline_timer.hpp下定义了
typedef basic_deadline_timer<boost::posix_time::ptime> deadline_timer;2在boost_1_59_0\boost\asio\deadline_timer.hpp下定义了模板类
class basic_deadline_timer
Name | Description |
---|---|
async_wait | Start an asynchronous wait on the timer. |
basic_deadline_timer | Constructor.Constructor to set a particular expiry time as an absolute time.Constructor to set a particular expiry time relative to now. |
cancel | Cancel any asynchronous operations that are waiting on the timer. |
cancel_one | Cancels one asynchronous operation that is waiting on the timer. |
expires_at | Get the timer's expiry time as an absolute time.Set the timer's expiry time as an absolute time. |
expires_from_now | Get the timer's expiry time relative to now.Set the timer's expiry time relative to now. |
get_io_service | Get the io_service associated with the object. |
wait | Perform a blocking wait on the timer. |
相关文章推荐
- 快速多边形填充算法
- 数据的处理方法及触摸屏终端
- SHELL编程实例
- ssh简介
- qt 调试日志安装
- Instruments-Automation: 通过命令行执行测试用例
- pysonar
- Programming Ability Test学习 3-05. 求链式线性表的倒数第K项(15)
- 羞耻羞耻羞耻!!!
- C++基本知识归纳(2)
- 【bzoj3175】 TJOI2013攻击装置 二分图最大独立集
- 3D打印技术之切片引擎(6)
- I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo
- 浅谈C++中指针和引用的区别
- 笔记122--关于WebView
- Linux中egrep sed awk的比较
- UITableView的简单使用
- sd/mmc的cmd命令
- android ADT Bundle 下载地址
- Modify the server ports