【C++】实现一个定时器
前言
实现一个周期性调用类。通过TaskTimer构造函数设置周期,通过setTimerFun传入要调用函数和参数,start启动,stop停止。比如要每30秒发送一个心跳包可以把发送包的函数传入定时器,定时器会创建一个线程周期性发送这个包。
实现
TaskTimer主要有开始、停止、设置调用函数与参数,三个方法。
- TaskTimer.h
1 #ifndef _TASKTIMER_H
2 #define _TASKTIMER_H
3 //计时器 实现循环注册
4 #include <iostream>
5 #include <thread>
6 #include <chrono>
7 #include <unistd.h>
8
9 //定时器调用的函数
10 typedef void(*timerFunCallBack)(void * param);
11
12 class TaskTimer{
13
14 public:
15 TaskTimer(unsigned long second);
16 ~TaskTimer();
17
18 void start();
19 void stop();
20
21 void setTimerFun(timerFunCallBack fun,void* param);
22 static void * timer(void * context);
23
24 private:
25 //回调函数
26 timerFunCallBack m_timerFun;
27 //回调函数参数
28 void * m_funParam;
29 //间隔
30 unsigned long m_timeSecond;
31 bool m_isStop;
32 std::thread m_thread;
33 };
34
35 #endif
- TaskTimer.cpp
1 #include "TaskTimer.h"
2 #include <sys/time.h>
3
4 TaskTimer::TaskTimer(unsigned long second):m_timeSecond(second),m_isStop(true){
5 m_timerFun = nullptr;
6 m_funParam = nullptr;
7
8 }
9 TaskTimer:: ~TaskTimer(){
10 stop();
11 }
12
13 void TaskTimer::start(){
14 //创建线程
15 m_isStop = false;
16 m_thread = std::thread(&TaskTimer::timer,(void*)this);
17 std::cout<<"thread create"<<std::endl;
18 }
19 void TaskTimer::stop(){
20 m_isStop = true;
21 if(m_thread.joinable()){
22 m_thread.join();
23 }
24 }
25
26 void TaskTimer::setTimerFun(timerFunCallBack fun,void* param){
27 m_timerFun = fun;
28 m_funParam = param;
29 }
30
31 void * TaskTimer::timer(void * taskTimer){
32 std::cout << "Timer thread function ENTERED!" << std::endl;
33 TaskTimer * pthis = (TaskTimer * )taskTimer;
34 if(nullptr == pthis){
35 return nullptr;
36 }
37 unsigned long curTime = 0;//当前时间
38 unsigned long lastTime = 0;//上次更新时间时间
39 struct timeval current;
40 while(!pthis->m_isStop){
41 gettimeofday(¤t,nullptr);
42 //换算成毫秒 tv_sec 秒 tv_usec 微妙
43 curTime = current.tv_sec+current.tv_usec/1000000;
44
45 // 处理时间回退情况
46 if (curTime < lastTime) {
47 lastTime = curTime;
48 continue;
49 }
50
51 if((curTime-lastTime) >= (pthis->m_timeSecond)){
52 lastTime = curTime;
53 if(pthis->m_timerFun != nullptr){
54 pthis->m_timerFun(pthis->m_funParam);
55 }
56 }else{
57 usleep(1000*1000);
58 continue;
59 }
60 }
61 return nullptr;
62 }
- main.cpp
main函数实现调用。
1 #include <iostream>
2 #include <string>
3 #include "TaskTimer.h"
4
5 static void func(void*param){
6 int * pi = (int*)param;
7 std::cout<<"hello !"<< *pi << std::endl;
8 }
9
10 int main(){
11
12 //TaskTimer* timer = new TaskTimer(3);
13 TaskTimer timer(1);
14 int param = 123;
15 int * ptr = ¶m;
16 timer.setTimerFun(func,(void *)ptr);
17 timer.start();
18
19 usleep(10000*1000);
20 timer.stop();
21 return 0;
22 }

浙公网安备 33010602011771号