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()
{
// TODO Auto-generated destructor stub
}
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);
}
//如果设置startInterval和periodicinterval,否则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);
}
//如果设置startInterval和periodicinterval,否则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();