/************************类功能描述**********************/
Master类:
1. 使用单例模式实现Master类
2. 用于管理定时器、创建定时器、销毁定时器
3. 定时器内存管理
MyTimer类:
1. 使用工厂模式实现定时器
2. 用于实现定时器的功能
constant.h:
定义定时器的ID标识
constant.h:
//定义回调函数指针
typedef void (*PFUNC)();
enum EM_TIMER_ID
{
//定时器的ID从1开始
EM_TIMER_START = 1,
EM_TIMER_END
};
Master.h:
pragma once
include “stdafx.h”
include “MyTimer.h”
include
class CMaster
{
public:
virtual ~CMaster(void);
static CMaster* GetMaster();
//设置一个定时器,并运行
void SetTimer(int nTimerID, int nTimeOut, PFUNC pFunc);
//停止特定定时器
void StopTimer(int nTimerID);
//启动定时器循环
void RunMaster();
//执行定时器
void RunTimer(CMyTimer* cTimer);
private:
CMaster(void);
static CMaster* m_cMaster;
std::vector
include “stdafx.h”
include “Master.h”
CMaster* CMaster::m_cMaster = NULL;
CMaster::CMaster(void)
{
m_IsRun = true;
}
CMaster::~CMaster(void)
{
for(std::vector< CMyTimer*>::iterator iter=m_vTimer.begin(); iter!=m_vTimer.end(); iter++)
{
delete *iter;
*iter = NULL;
}
m_vTimer.clear();
if(m_cMaster)
{
delete m_cMaster;
m_cMaster = NULL;
}
}
CMaster* CMaster::GetMaster()
{
if(m_cMaster == NULL)
{
m_cMaster = new CMaster();
}
return m_cMaster;
}
//设置一个定时器,并运行
void CMaster::SetTimer(int nTimerID, int nTimeOut, PFUNC pFunc)
{
CMyTimer* cTimer = CMyTimer::CreatTimer(nTimerID, nTimeOut, pFunc);
if(cTimer)
{
m_Mutex.lock();
m_vTimer.push_back(cTimer);
m_Mutex.unlock();
}
}
//停止特定定时器
void CMaster::StopTimer(int nTimerID)
{
m_Mutex.lock();
for(std::vector< CMyTimer*>::iterator iter=m_vTimer.begin(); iter!=m_vTimer.end(); )
{
if(nTimerID == ((*iter)->GetTimerID()) )
{
iter = m_vTimer.erase(iter);
break;
}
else
{
iter++;
}
}
m_Mutex.unlock();
}
//启动定时器循环
void CMaster::RunTimer(CMyTimer* cTimer)
{
if(cTimer)
{
cTimer->RunTimer();
}
}
//启动定时器循环
void CMaster::RunMaster()
{
while(m_IsRun)
{
DWORD nTime = GetTickCount();
m_Mutex.lock();
for(std::vector< CMyTimer*>::iterator iter=m_vTimer.begin(); iter!=m_vTimer.end(); )
{
if(nTime >= (*iter)->GetOutTime())
{
std::cout << “ID: “<< (*iter)->GetTimerID() << std::endl;
std::thread td(&CMaster::RunTimer, this, *iter);
td.detach();
iter = m_vTimer.erase(iter);
if(m_vTimer.size() == 0)
m_IsRun = false;
}
else
{
iter++;
}
}
m_Mutex.unlock();
Sleep(500);
}
}
CMyTimer.h:
pragma once
include “time.h”
include “stdafx.h”
class CMyTimer
{
public:
CMyTimer();
virtual ~CMyTimer(void);
//创建定时器
static CMyTimer* CreatTimer(int nTimerID, int nTimeOut, PFUNC pFunc);
//初始化数据
void Initialize();
//设置定时器触发时间
void SetTime(int nTime);
//启动定时器
void StartTimer();
//终止定时器
void StopTimer();
//获取定时器唯一标识
int GetTimerID();
//定时器启动后计时函数
void RunTimer();
//获取超时时间
DWORD GetOutTime() { return m_nTimeOut; }
private:
//定时器触发时的回调函数
static void CallBackFunc(PFUNC pFunc) { m_pFunc = pFunc; }
private:
DWORD m_nTimeCounter; //定时器内部计时数据
int m_nTimeOut; //定时器触发时间
int m_nTimerID; //定时器标识(唯一)
bool m_bIsRun; //定时器运行标志
static PFUNC m_pFunc; //回调函数指针
};
CMyTimer.cpp:
include “stdafx.h”
include “MyTimer.h”
include
PFUNC CMyTimer::m_pFunc = NULL;
CMyTimer::CMyTimer()
{
Initialize();
}
CMyTimer::~CMyTimer(void)
{
}
//创建定时器
CMyTimer* CMyTimer::CreatTimer(int nTimerID, int nTimeOut, PFUNC pFunc)
{
if((0 >= nTimerID) || (NULL == pFunc))
{
return NULL;
}
CMyTimer* pMyTimer = new CMyTimer();
pMyTimer->m_nTimerID = nTimerID;
pMyTimer->m_nTimeOut = nTimeOut*1000 + GetTickCount();
CallBackFunc(pFunc);
return pMyTimer;
}
//初始化数据
void CMyTimer::Initialize()
{
m_nTimeCounter = 0;
m_nTimeOut = 0;
m_nTimerID = 0;
m_bIsRun = true;
m_pFunc = NULL;
}
//设置定时器触发时间
void CMyTimer::SetTime(int nTime)
{
if(nTime < 0)
{
m_nTimeOut = 0;
}
else
{
m_nTimeOut = nTime;
}
}
//启动定时器
void CMyTimer::StartTimer()
{
RunTimer();
}
//终止定时器
void CMyTimer::StopTimer()
{
m_bIsRun = false;
}
//获取定时器唯一标识
int CMyTimer::GetTimerID()
{
return m_nTimerID;
}
//定时器启动后计时函数
void CMyTimer::RunTimer()
{
if(m_pFunc)
{
m_pFunc();
}
}
main函数:
// 自定义定时器.cpp : 定义控制台应用程序的入口点。
//
include “stdafx.h”
include “Master.h”
void OutPut()
{
std::cout<<”定时器时间到咯: ” << GetTickCount() << std::endl;
}
void RunTimer1()
{
CMaster* mster = CMaster::GetMaster();
mster->SetTimer(EM_TIMER_START, 3, OutPut);
}
void RunTimer2()
{
CMaster* mster = CMaster::GetMaster();
mster->SetTimer(EM_TIMER_END, 6, OutPut);
}
void RunTimer3()
{
CMaster* mster = CMaster::GetMaster();
mster->SetTimer(EM_TIMER_END, 8, OutPut);
}
void RunTimer4()
{
CMaster* mster = CMaster::GetMaster();
mster->SetTimer(EM_TIMER_END, 9, OutPut);
}
void RunTimer5()
{
CMaster* mster = CMaster::GetMaster();
mster->RunMaster();
}
void RunTimer7()
{
CMaster* mster = CMaster::GetMaster();
mster->SetTimer(EM_TIMER_START, 3, OutPut);
mster->SetTimer(EM_TIMER_END, 6, OutPut);
mster->SetTimer(3, 20, OutPut);
mster->SetTimer(4, 7, OutPut);
mster->SetTimer(5, 8, OutPut);
mster->SetTimer(6, 9, OutPut);
mster->SetTimer(7, 10, OutPut);
mster->SetTimer(8, 11, OutPut);
mster->SetTimer(9, 12, OutPut);
//mster->StopTimer(6);
//mster->StopTimer(8);
//mster->StopTimer(4);
}
int _tmain(int argc, _TCHAR* argv[])
{
std::thread td1(RunTimer7);
std::thread td2(RunTimer5);
td1.join();
td2.join();
system("pause");
return 0;
}
执行结果: