西樵强记 發表於 2026-2-10 22:23:00

【C++】实现一个定时器

<h2 id="前言">前言</h2>
<p>实现一个周期性调用类。通过TaskTimer构造函数设置周期,通过setTimerFun传入要调用函数和参数,start启动,stop停止。比如要每30秒发送一个心跳包可以把发送包的函数传入定时器,定时器会创建一个线程周期性发送这个包。</p>
<h2 id="实现">实现</h2>
<p>TaskTimer主要有开始、停止、设置调用函数与参数,三个方法。</p>
<ul>
<li>TaskTimer.h</li>
</ul>
<pre><code class="language-C++">1 #ifndef_TASKTIMER_H
2 #define _TASKTIMER_H
3 //计时器 实现循环注册
4 #include &lt;iostream&gt;
5 #include &lt;thread&gt;
6 #include &lt;chrono&gt;
7 #include &lt;unistd.h&gt;
8
9 //定时器调用的函数
10 typedef void(*timerFunCallBack)(void * param);
11
12 classTaskTimer{
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

</code></pre>
<ul>
<li>TaskTimer.cpp</li>
</ul>
<pre><code class="language-C++"> 1 #include "TaskTimer.h"
2 #include &lt;sys/time.h&gt;
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(&amp;TaskTimer::timer,(void*)this);
17   std::cout&lt;&lt;"thread create"&lt;&lt;std::endl;
18 }
19 void TaskTimer::stop(){
20   m_isStop = true;
21   if(m_thread.joinable()){
22         m_thread.join();
23   }
24 }
25
26void 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 &lt;&lt; "Timer thread function ENTERED!" &lt;&lt; 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   structtimeval current;
40   while(!pthis-&gt;m_isStop){
41         gettimeofday(&amp;current,nullptr);
42         //换算成毫秒 tv_sec 秒 tv_usec 微妙
43         curTime = current.tv_sec+current.tv_usec/1000000;
44
45          // 处理时间回退情况
46          if (curTime &lt; lastTime) {
47                  lastTime = curTime;
48                  continue;
49         }
50
51         if((curTime-lastTime) &gt;= (pthis-&gt;m_timeSecond)){
52             lastTime = curTime;
53             if(pthis-&gt;m_timerFun != nullptr){
54               pthis-&gt;m_timerFun(pthis-&gt;m_funParam);
55             }
56         }else{
57               usleep(1000*1000);
58               continue;
59         }
60   }
61   return nullptr;
62 }

</code></pre>
<ul>
<li>main.cpp</li>
</ul>
<p>main函数实现调用。</p>
<pre><code class="language-C++">1 #include &lt;iostream&gt;
2 #include &lt;string&gt;
3 #include "TaskTimer.h"
4
5 static void func(void*param){
6         int * pi = (int*)param;
7         std::cout&lt;&lt;"hello !"&lt;&lt; *pi &lt;&lt; std::endl;
8 }
9
10 int main(){
11
12         //TaskTimer* timer = new TaskTimer(3);
13         TaskTimer timer(1);
14         intparam= 123;
15         int * ptr = &amp;param;
16         timer.setTimerFun(func,(void *)ptr);
17         timer.start();
18
19         usleep(10000*1000);
20         timer.stop();
21         return 0;
22 }

</code></pre><br><br>
来源:https://www.cnblogs.com/hjk-airl/p/19489397
頁: [1]
查看完整版本: 【C++】实现一个定时器