Poco Timer

 Poco::Timer

Poco::Timer 提供了定时任务的功能,从线程池中创建一个线程,每隔一段时间让线程中的主体代码执行一次。其余时间此线程阻塞在Event.trywait(long seconds)调用上,来达到定时的目的

PocoTimer.h

#ifndef POCOTIMER_H_

#define POCOTIMER_H_

#include "Poco/Timer.h"

using Poco::Timer;         // 使用Timer基类的成员

using Poco::TimerCallback; // 使用TimerCallback基类的成员

typedef std::function<void()> TimeOutCallback;回调函数

class PocoTimer{

public:

         PocoTimer();

         virtual ~PocoTimer();

         void start(int nmillseconds ,TimeOutCallback callback);

         void stop();

         void onTimer(Timer& timer);

private:

         Poco::Timer* m_ptrTimer;

         TimeOutCallback m_ptrCallback;

         bool m_bStart;

};

#endif /* POCOTIMER_H_ */

 

PocoTimer.cpp

#include "PocoTimer.h"

#include <iostream>

PocoTimer::PocoTimer(){

         m_ptrTimer = nullptr;

         m_bStart = false;

}

 

PocoTimer::~PocoTimer(){

}

 

int getTickCountSecond(){

         struct timespec ts;

         ts.tv_sec = 0;

         clock_gettime(CLOCK_MONOTONIC,&ts);

         return ts.tv_sec;

}

 

void PocoTimer::start(int nmillseconds,TimeOutCallback callback){

         if(m_bStart){//如果定时器已经启动,需要重新启动

                   m_ptrCallback = callback;

                   std::cout << "PocoTimer::restart" <<  getTickCountSecond() << std::endl;

                   //Sets a new periodic interval and restarts the timer. An interval of 0 will stop the timer.

                   m_ptrTimer->restart(nmillseconds);

                   return;

         }

         m_bStart = true;

         if(m_ptrTimer == nullptr){

//创建定时器,第一个参数表示定时器几秒后启动,
第二个参数代表时间间隔为nmillseconds豪秒

                   m_ptrTimer = new Poco::Timer(nmillseconds ,nmillseconds);

         }

         //如果设置startIntervalperiodicinterval,否则stop后再start只会执行一次

         m_ptrTimer->setStartInterval(nmillseconds);

         m_ptrTimer->setPeriodicInterval(nmillseconds);

         m_ptrCallback = callback;

         TimerCallback<PocoTimer> tc(*this, &PocoTimer::onTimer);

         std::cout << "PocoTimer::start" <<  getTickCountSecond() << std::endl;

         m_ptrTimer->start(tc);

}

void PocoTimer::stop(){

         m_bStart = false;

         m_ptrTimer->stop();

         std::cout << "PocoTimer::stop" << std::endl;

}

void PocoTimer::onTimer(Timer& timer){

         std::cout << "PocoTimer::onTimer" <<  getTickCountSecond() << std::endl;

         if(m_ptrCallback != nullptr){

                   m_ptrCallback();

         }

}

Main.cpp

int main(){   

         PocoTimer pocotimer;

         pocotimer.start(1000 ,[](){

                   std::cout << "timeout" << std::endl;

         });

 

 测试1,start定时器3s后启动定时器,然后间隔1s超时

void PocoTimer::start(int nmillseconds,TimeOutCallback callback){

         if(m_bStart){

                   m_ptrCallback = callback;

                   std::cout << "PocoTimer::restart" <<  getTickCountSecond() << std::endl;

                   //Sets a new periodic interval and restarts the timer. An interval of 0 will stop the timer.

                   m_ptrTimer->restart(nmillseconds);

                   return;

         }

         m_bStart = true;

         if(m_ptrTimer == nullptr){

                   m_ptrTimer = new Poco::Timer(0 ,nmillseconds);

         }

         //如果设置startIntervalperiodicinterval,否则stop后再start只会执行一次

         m_ptrTimer->setStartInterval(3000);

         m_ptrTimer->setPeriodicInterval(nmillseconds);

         m_ptrCallback = callback;

         TimerCallback<PocoTimer> tc(*this, &PocoTimer::onTimer);

         std::cout << "PocoTimer::start" <<  getTickCountSecond() << std::endl;

         m_ptrTimer->start(tc);

}

 

 多次启动定时器

int main(){

         PocoTimer pocotimer;

         pocotimer.start(1000 ,[](){

                   std::cout << "timeout" << std::endl;

         });

         sleep(2);

         pocotimer.start(2000 ,[](){

                   std::cout << "timeout" << std::endl;

         });

         sleep(7);

         pocotimer.stop();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值