【08】 Boost库学习笔记之定时器(Timer)
来源:互联网 发布:韩子高网络剧 编辑:程序博客网 时间:2024/06/14 19:26
一 示例代码
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.
- 【08】 Boost库学习笔记之定时器(Timer)
- 初探boost之timer库学习笔记
- 【boost学习】之boost::asio(1)——Timer定时器
- Boost学习之Timer库
- QtQuick学习笔记之QML定时器Timer
- 【Boost】boost库中timer定时器
- boost库学习之 timer库
- boost--timer库笔记
- Boost库学习----timer
- boost-timer库学习
- ACE学习之定时器(Timer)
- ACE学习之定时器(Timer)
- ACE学习之定时器(Timer)
- boost学习之二(时间 timer库)
- 线程学习笔记(八)-定时器(Timer)
- 字符设备之笔记-定时器(timer)
- Boost::timer 定时器的使用
- boost之学习备注浅析timer篇
- MySQL索引原理及慢查询优化
- /usr/bin/ld: cannot find -lmysqlclient_r
- noip2000乘积最大 (高精度,动态规划)
- [LeetCode] N-Queens
- g++4.9仍然没实现put_time
- 【08】 Boost库学习笔记之定时器(Timer)
- [BATCH]DOS批处理--根据当前日期和时间来创建文件夹和文件名
- 关于做系统集成项目的思考
- usleep - 延迟以微秒为单位的时间
- git-log
- 笔记122--关于WebView
- BootStrap 按钮加载状态改变
- noip2000单词接龙(搜素)
- POJ---2524-Ubiquitous Religions(并查集)