#ifndef _MYTIMER_H_#define _MYTIMER_H_#include
#include "MyTimer.h"#include#include using namespace std;CMyTimer::CMyTimer(void){ m_nTimerID = 0;}CMyTimer::~CMyTimer(void){}TimerMap CMyTimer::TimerMap;bool CMyTimer::m_bInitTime = false;CLock CMyTimer::m_lock;void CMyTimer::SetMyTimerOnce(UINT nElapse,DWORD dwUser){ if (m_bInitTime) { return; } if (NULL != ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_ONESHOT)) { m_bInitTime = true; }}void CMyTimer::SetMyTimerPeriod(UINT nElapse,DWORD dwUser){ if (NULL != ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_PERIODIC)) { m_bInitTime = true; }}void CALLBACK CMyTimer::OnTime(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2){ CSignedLock lock(&m_lock); for (auto iter = TimerMap.begin(); iter != TimerMap.end();) { iter->second.uTimeVal += TIMER_PERIOD; if (iter->second.uTimeVal >= iter->second.uTimeFinal) { if (E_TIME_ONESHOT == iter->second.uType) { iter->second.pCTimer->ontime(iter->first); TimerMap.erase(iter++); continue; } if (E_TIME_PERIOD == iter->second.uType) { iter->second.uTimeVal = 0; if (false == iter->second.pCTimer->ontime(iter->first)) { TimerMap.erase(iter++); continue; } } } iter++; } return;}void CMyTimer:: KillMyTimer(){ ::timeKillEvent(m_nTimerID); m_bInitTime = false;}void CMyTimer::SetOneTimer(UINT nTimeID,UINT nElapse){ CSignedLock lock(&m_lock); auto iter = TimerMap.find(nTimeID); if (iter == TimerMap.end()) { TimerMap[nTimeID].pCTimer = this; TimerMap[nTimeID].uTimeFinal = nElapse; TimerMap[nTimeID].uTimeVal = 0; TimerMap[nTimeID].uType = E_TIME_ONESHOT; }}void CMyTimer::SetTimer(UINT nTimeID,UINT nElapse){ CSignedLock lock(&m_lock); auto iter = TimerMap.find(nTimeID); if (iter == TimerMap.end()) { TimerMap[nTimeID].pCTimer = this; TimerMap[nTimeID].uTimeFinal = nElapse; TimerMap[nTimeID].uTimeVal = 0; TimerMap[nTimeID].uType = E_TIME_PERIOD; }}
#ifndef _Work_H_#define _Work_H_#include "MyTimer.h"enum MyEnum{ E_TIME_ID_1 = 1, E_TIME_ID_2 = 2, E_TIME_ID_3 = 3,};class Work :public CMyTimer{public: Work(void); ~Work(void); bool Init(); void Run(); bool unInit(); void onTime(); bool time_fun_1(); bool time_fun_2(); bool time_fun_3(); bool ontime(UINT nTimeID);};#endif//_Work_H_
#include "Work.h"#includeusing namespace std;Work::Work(void){}Work::~Work(void){}bool Work::Init(){ if (!CMyTimer::m_bInitTime) { CMyTimer::SetMyTimerPeriod(TIMER_PERIOD); } return true;}void Work::Run(){ SetOneTimer(E_TIME_ID_1,1000); SetTimer(E_TIME_ID_2,1000);}bool Work::unInit(){ return true;}bool Work::time_fun_1(){ std::cout<<"只执行一次的定时器"<<__LINE__<
///普通锁定类#ifndef _CLOCK_H_#define _CLOCK_H_#includeclass CLock{ friend class CSignedLock;private: CRITICAL_SECTION m_csLock; ///关键段 ///函数定义public: ///构造函数 inline CLock() { ::InitializeCriticalSection(&m_csLock); } ///析构函数 inline ~CLock() { ::DeleteCriticalSection(&m_csLock); } ///功能函数private: ///锁定函数 inline void Lock() { ::EnterCriticalSection(&m_csLock); } ///解锁函数 inline void UnLock() { ::LeaveCriticalSection(&m_csLock); }};class CSignedLock{ ///变量定义private: CLock * m_pLock; ///锁定对象 int m_iLockCount; ///锁定计数 ///函数定义public: ///构造函数 CSignedLock(CLock * pLock) { m_iLockCount = 0; m_pLock = pLock; Lock(); } ///析构函数 ~CSignedLock() { UnLock(); }public: ///锁定函数 inline void Lock() { if (m_pLock) { m_iLockCount++; m_pLock->Lock(); } } ///解锁函数 inline void UnLock() { if (m_pLock) { m_iLockCount--; m_pLock->UnLock(); } }};#endif //_CLOCK_H_
#ifndef _TTYPE_H_#define _TTYPE_H_typedef unsigned int UINT;#define saferelease(p) {\ if(nullptr != p) {\ delete p ; \ p = nullptr;\ }\}#endif
#include "work.h"#include#include using namespace std;Work work ;int main(){ work.Init(); work.Run(); system("pause"); cout<