【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.



0 0