C++ 实现的定时器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 实现的定时器相关的知识,希望对你有一定的参考价值。
主要由两个类组成
1 cTimerMgr 管理所有的定时器对象 是一个单例对象
2 cTimerObj 每一个定时器对象都必须继承这个类
重写
virtual TimerResultEnum onTimer(int nTimeId)
{
return Timer_Exit;
}
源代码如下:
1 cTimerMgr.h
1 /* 2 * cTimerObj.h 3 * 4 * Created on: Jan 27, 2016 5 * Author: work 6 */ 7 8 #ifndef CTIMEROBJ_H_ 9 #define CTIMEROBJ_H_ 10 11 //定时器类型 12 //enum TimerTypeEnum 13 //{ 14 // timeType_interval, //间隔时间 15 // timeType_point, //指定时间点 16 //}; 17 18 //定时器结果类型 19 enum TimerResultEnum 20 { 21 Timer_Exit, //取消timer 22 Timer_Continue, //继续timer 23 }; 24 25 class cTimerObj 26 { 27 public: 28 cTimerObj(); 29 virtual ~cTimerObj(); 30 31 public: 32 int AddTimer(int nInterval); 33 bool DeleteTimer(int nTimerId); 34 bool SetTimer(int nPointTime);//指定时间点触发 35 36 virtual TimerResultEnum onTimer(int nTimeId) 37 { 38 return Timer_Exit; 39 } 40 41 }; 42 43 #endif /* CTIMEROBJ_H_ */
cTimerMgr.cpp
1 /* 2 * cTimerMgr.cpp 3 * 4 * Created on: Jan 27, 2016 5 * Author: work 6 */ 7 8 #include "cTimerMgr.h" 9 #include "time.h" 10 11 IM_INSTANCE(cTimerMgr) 12 cTimerMgr::cTimerMgr() 13 { 14 // TODO Auto-generated constructor stub 15 16 } 17 18 cTimerMgr::~cTimerMgr() 19 { 20 // TODO Auto-generated destructor stub 21 } 22 23 int cTimerMgr::AddTimer(int nInterval, cTimerObj* pObj) 24 { 25 if(nInterval <= 0 || pObj == 0) 26 { 27 return 0; 28 } 29 30 long lNow = time(0); 31 int nTimerID = GetTimerId(); 32 TimerNode* pTimeNode = new TimerNode(); 33 pTimeNode->_interval = nInterval; 34 pTimeNode->_timer_id = nTimerID; 35 pTimeNode->_pObj = pObj; 36 pTimeNode->_time_dest = lNow + nInterval; 37 m_queue.push(pTimeNode); 38 m_mapNode[nTimerID] = pTimeNode; 39 return nTimerID; 40 } 41 42 bool cTimerMgr::DeleteTimer(int nTimerId) 43 { 44 itmapTimeNode it = m_mapNode.find(nTimerId); 45 if(it == m_mapNode.end()) 46 { 47 return false; 48 } 49 it->second->_cancelled = true; 50 m_mapNode.erase(it); 51 return true; 52 } 53 54 int cTimerMgr::GetTimerId() 55 { 56 int static nid = 0; 57 return nid++; 58 } 59 60 void cTimerMgr::removeMap(int nTimerId) 61 { 62 itmapTimeNode it = m_mapNode.find(nTimerId); 63 if(it == m_mapNode.end()) 64 { 65 return ; 66 } 67 m_mapNode.erase(it); 68 } 69 70 void cTimerMgr::process_timer() 71 { 72 long now_time = time(0); 73 74 while(!m_queue.empty()) 75 { 76 TimerNode * t = m_queue.top(); 77 if(t->_time_dest <= now_time) 78 { 79 m_queue.pop(); 80 if(t->_cancelled == false) 81 { 82 switch(t->_pObj->onTimer(t->_timer_id)) 83 { 84 case Timer_Exit: 85 removeMap(t->_timer_id); 86 delete t; 87 break; 88 case Timer_Continue: 89 t->_time_dest = now_time + t->_interval; 90 m_queue.push(t); 91 break; 92 default: 93 break; 94 } 95 } 96 else 97 { 98 removeMap(t->_timer_id); 99 delete t; 100 } 101 } 102 else 103 { 104 break; 105 } 106 } 107 }
2 cTimerObj.h
1 /* 2 * cTimerObj.h 3 * 4 * Created on: Jan 27, 2016 5 * Author: work 6 */ 7 8 #ifndef CTIMEROBJ_H_ 9 #define CTIMEROBJ_H_ 10 11 //定时器类型 12 //enum TimerTypeEnum 13 //{ 14 // timeType_interval, //间隔时间 15 // timeType_point, //指定时间点 16 //}; 17 18 //定时器结果类型 19 enum TimerResultEnum 20 { 21 Timer_Exit, //取消timer 22 Timer_Continue, //继续timer 23 }; 24 25 class cTimerObj 26 { 27 public: 28 cTimerObj(); 29 virtual ~cTimerObj(); 30 31 public: 32 int AddTimer(int nInterval); 33 bool DeleteTimer(int nTimerId); 34 bool SetTimer(int nPointTime);//指定时间点触发 35 36 virtual TimerResultEnum onTimer(int nTimeId) 37 { 38 return Timer_Exit; 39 } 40 41 }; 42 43 #endif /* CTIMEROBJ_H_ */
cTimerObj.cpp
1 /* 2 * cTimerObj.cpp 3 * 4 * Created on: Jan 27, 2016 5 * Author: work 6 */ 7 8 #include "cTimerObj.h" 9 #include "cTimerMgr.h" 10 11 cTimerObj::cTimerObj() 12 { 13 // TODO Auto-generated constructor stub 14 15 } 16 17 cTimerObj::~cTimerObj() 18 { 19 // TODO Auto-generated destructor stub 20 } 21 22 int cTimerObj::AddTimer(int nInterval) 23 { 24 return cTimerMgr::Inst().AddTimer(nInterval, this); 25 } 26 27 bool cTimerObj::DeleteTimer(int nTimerId) 28 { 29 return cTimerMgr::Inst().DeleteTimer(nTimerId); 30 }
3 客户程序
使用例子:
CApple.h
1 /* 2 * cApple.h 3 * 4 * Created on: Jan 28, 2016 5 * Author: work 6 */ 7 8 #ifndef CAPPLE_H_ 9 #define CAPPLE_H_ 10 #include "cTimerObj.h" 11 12 class cApple : public cTimerObj 13 { 14 public: 15 cApple(); 16 virtual ~cApple(); 17 18 public: 19 virtual TimerResultEnum onTimer(int nTimeId); 20 21 private: 22 int m_id1; 23 int m_id2; 24 25 void onHandle1(); 26 void onHandle2(); 27 }; 28 29 #endif /* CAPPLE_H_ */
CApple.cpp
1 /* 2 * cApple.cpp 3 * 4 * Created on: Jan 28, 2016 5 * Author: work 6 */ 7 8 #include "cApple.h" 9 #include "stdio.h" 10 11 cApple::cApple() 12 { 13 // TODO Auto-generated constructor stub 14 m_id1 = AddTimer(4); 15 m_id2 = AddTimer(10); 16 } 17 18 cApple::~cApple() { 19 // TODO Auto-generated destructor stub 20 } 21 22 23 TimerResultEnum cApple::onTimer(int nTimeId) 24 { 25 if(nTimeId == m_id1) 26 { 27 onHandle1(); 28 return Timer_Continue; 29 } 30 else if(nTimeId == m_id2) 31 { 32 onHandle2(); 33 return Timer_Exit; 34 } 35 36 return Timer_Exit; 37 } 38 void cApple::onHandle1() 39 { 40 printf("111111111111111111111111111111111\n"); 41 } 42 43 void cApple::onHandle2() 44 { 45 printf("222222222222222222222222222222222\n"); 46 }
4 main 函数 每帧循环调用
1 //============================================================================ 2 // Name : timeMgr.cpp 3 // Author : fangjunmin 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include "cApple.h" 11 #include "cTimerMgr.h" 12 13 using namespace std; 14 15 int main() 16 { 17 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! 18 cApple apple; 19 while(1) 20 { 21 cTimerMgr::Inst().process_timer(); 22 } 23 return 0; 24 }
以上是关于C++ 实现的定时器的主要内容,如果未能解决你的问题,请参考以下文章