您的位置:首页 > 其它

【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
NameDescription
async_waitStart an asynchronous wait on the timer.
basic_deadline_timerConstructor.Constructor to set a particular expiry time as an absolute time.Constructor to set a particular expiry time relative to now.
cancelCancel any asynchronous operations that are waiting on the timer.
cancel_oneCancels one asynchronous operation that is waiting on the timer.
expires_atGet the timer's expiry time as an absolute time.Set the timer's expiry time as an absolute time.
expires_from_nowGet the timer's expiry time relative to now.Set the timer's expiry time relative to now.
get_io_serviceGet the io_service associated with the object.
waitPerform a blocking wait on the timer.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: