ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ 11 实现定时器

2021-06-13 20:34:56  阅读:266  来源: 互联网

标签:11 std 定时器 void interval MyTimer C++ func id


用C++写了个定时器。

项目的需求是原来Windows的程序,用到了windows APi的 SetTimerKillTimer 来创建和销毁定时器,现在要移植代码到Linux,实现与其相似的功能。

首先创建一个Timer类,管理单个定时器。

 1 typedef std::function<void()> TimerFunc;
 2 
 3 #define TIMER_ID unsigned long
 4 #define INTERVAL unsigned int
 5 
 6 class MyTimer{
 7 public:
 8     MyTimer(TIMER_ID _id, INTERVAL _interval, TimerFunc _func);
 9     void stopTimer();
10     void startTimer();
11     ~MyTimer();
12 private:
13     TIMER_ID m_id;
14     INTERVAL m_interval;
15     bool m_stop;
16     TimerFunc m_func;
17     std::future<int> m_future;
18 };

用一个全局hash表来管理Timer,并定义一个全局的mutex实现线程锁

1 std::unordered_map<TIMER_ID, MyTimer*> TimerMap;
2 std::mutex g_mutex;

定义一个TimerServer类实现接口:

 1 class TimerService
 2 {
 3 public:
 4     static TIMER_ID setTimer(INTERVAL _interval, TimerFunc _func);
 5     static void killTimer(TIMER_ID _id);
 6 };
 7 
 8 static TIMER_ID NewTimerID = 0;
 9 
10 TIMER_ID TimerService::setTimer(INTERVAL _interval, TimerFunc _func){
11     MyTimer* pTimer = new MyTimer(NewTimerID, _interval, _func);
12     TimerMap[NewTimerID] = pTimer;
13     pTimer->startTimer();
14     return NewTimerID++;
15 }
16 
17 void TimerService::killTimer(unsigned long _id)
18 {
19     MyTimer* pTimer = TimerMap.at(_id);
20     if (pTimer){
21         pTimer->stopTimer();
22         delete pTimer;
23         TimerMap.erase(_id);
24     }
25 }

Timer内部的方法实现:

 1 MyTimer::MyTimer(unsigned long _id, unsigned int _interval, TimerFunc _func)
 2 {
 3     m_id = _id;
 4     m_interval = _interval;
 5     m_func = _func;
 6     m_stop = false;
 7 }
 8 
 9 void MyTimer::startTimer()
10 {
11     m_future = std::async(std::launch::async, [this](){
12         while(true){
13             if(m_stop)
14                 break;
15             std::this_thread::sleep_for(std::chrono::milliseconds(m_interval));
16             std::lock_guard<std::mutex> lock(g_mutex);
17             m_func();
18         }
19         return 0;
20     });
21 }
22 
23 void MyTimer::stopTimer()
24 {
25     m_stop = true;
26     m_future.wait();
27 }
28 
29 MyTimer::~MyTimer()
30 {
31 
32 }

测试:

 1 #include <iostream>
 2 #include "mytimer.h"
 3 #include <chrono>
 4 #include <functional>
 5 
 6 using namespace std;
 7 
 8 void printWorld() {
 9     cout << "world" << endl;
10 }
11 
12 class Router{
13 public:
14     void countSelf() {
15         cout << "Count is " << count << endl;
16         count++;
17     }
18 
19     Router(){
20         count = 0;
21         id = TimerService::setTimer(1000, std::bind(&Router::countSelf, this));
22     }
23     ~Router() {
24         TimerService::killTimer(id);
25     }
26 
27 private:
28     unsigned int count;
29     unsigned long id;
30 };
31 
32 int main()
33 {
34     auto l1 = TimerService::setTimer(1000, [](){cout << "hello" << endl;});
35     auto l2 = TimerService::setTimer(1000, std::bind(printWorld));
36     Router *r = new Router;
37 
38     std::this_thread::sleep_for(std::chrono::seconds(3));
39     TimerService::killTimer(l2);
40     delete r;
41     std::this_thread::sleep_for(std::chrono::seconds(5));
42     return 0;
43 }

Github

 

标签:11,std,定时器,void,interval,MyTimer,C++,func,id
来源: https://www.cnblogs.com/Asp1rant/p/14881089.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有