zoukankan      html  css  js  c++  java
  • (原创)boost定时器类

            最近,在写一个小项目,里面需要一个可以检测超时的东西,刚好再学boost(boss告诉我STL效率好低,我也不知道是不是),打算用boost写一个。代码贴出来,大家多指教指教。

    #ifndef TIMEOUTCONTROLLER_HPP_
    #define TIMEOUTCONTROLLER_HPP_
    
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/function.hpp>
    
    //超时控制器类
    class TimeoutController {
    public:
        /**
         * 超时控制器构造函数
         * @param ios 异步I/O对象
         * @param callbackFunc 超时处理回调函数
         * @param uiWaitSec 定时器间隔等待时间,单位:秒
         */
        explicit TimeoutController(boost::asio::io_service &ios,
                boost::function<void()> callbackFunc, unsigned int uiWaitSec) :
                timer(ios, boost::posix_time::seconds(uiWaitSec)) {
            timeoutHandle = callbackFunc;
            m_uiWaitSec = uiWaitSec;
            timer.async_wait(
                    boost::bind(&TimeoutController::onTime, this,
                            boost::asio::placeholders::error));
        }
        /**
         * 析构函数
         */
        ~TimeoutController() {
            timer.cancel();
        }
        /**
         * 定时器响应函数
         * @param error_code 定时器异常错误信息
         */
        inline void onTime(const boost::system::error_code&) {
            timeoutHandle();
            timer.expires_at(
                    timer.expires_at() + boost::posix_time::seconds(m_uiWaitSec));
            timer.async_wait(
                    boost::bind(&TimeoutController::onTime, this,
                            boost::asio::placeholders::error));
        }
    
    private:
        unsigned int m_uiWaitSec;                   //定时间间隔等待时间
        boost::asio::deadline_timer timer;         //asio定时器
        boost::function<void()> timeoutHandle;    //超时处理回调函数
    };
    
    
    
    #endif /* TIMEOUTCONTROLLER_HPP_ */

    欢迎转载(需保留此句),原文地址:http://www.cnblogs.com/wycnb/

  • 相关阅读:
    [Sdoi2016]征途
    何时会发生db file sequential read等待事件?
    oracle分布式事务总结
    db file scattered read 等待事件
    db file sequential read (数据文件顺序读取)
    INDEX_JOIN
    Oracle优化器介绍
    GC Buffer Busy Waits处理(转载)
    Oracle V$SESSION详解
    Oracle锁表(转载)
  • 原文地址:https://www.cnblogs.com/wycnb/p/4669556.html
Copyright © 2011-2022 走看看