您的位置:首页 > 编程语言 > C语言/C++

C++实现模拟定时器

2016-05-18 15:47 961 查看
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <map>
#include <new>
using namespace std;
/****************** Timer ****************************/
typedef void (*TimeOutProcess)(int count);
typedef int ID;
typedef struct TimerInfo
{
int count;
TimeOutProcess timerCb;
}stTmInfo;

class Timer
{
public:
Timer() : TimerIsCreate(0) {}
~Timer(){ TimerIsCreate = 0; }
int CreateTimer(ID TimerID, stTmInfo *timerInfo);
void StartTimer(ID TimerID);
void DeleteTimer(ID TimerID);
static map<ID, stTmInfo*> InitMap();
private:
static int maxID;
static map<ID, stTmInfo*> TimerMap;
int TimerIsCreate;
};

int Timer::maxID = 100;

map<ID, stTmInfo*> Timer::InitMap()
{
map<ID, stTmInfo*> tmp;
for(int i = 0; i < maxID; i++){
//tmp[i] = NULL;
tmp.insert(make_pair(i, (stTmInfo*)NULL));
}
return tmp;
}

map<ID, stTmInfo*> Timer::TimerMap(InitMap());

int Timer::CreateTimer(ID TimerID, stTmInfo *timerInfo)
{
if((timerInfo->timerCb == NULL)
|| (timerInfo->count <= 0)
|| (TimerID < 0)
|| (TimerID >= maxID)){

/* 异常例子 */
try
{
if(timerInfo->timerCb == NULL)
{
throw *timerInfo;
}
}
catch (stTmInfo& ex)  /* 通过引用传递异常 */
{
if(ex.timerCb == NULL)
cout << "&&&&&&&&&Timer callback function not define!!!" << endl;
}

return -1;
}

if(TimerMap[TimerID] == NULL){
if(TimerIsCreate){
cout << "one objection can create only one timer!!!" << endl;
return 0;
}
TimerMap[TimerID] = timerInfo;
}else{
cout << "Timer with TimerID = " << TimerID << " has exist!!!" << endl;
return 0;
}
TimerIsCreate = 1;
cout << "create Timer with TimerID = " << TimerID << " success!!!" << endl;
return 0;
}

void Timer::StartTimer(ID TimerID)
{
if(TimerID < 0 || TimerID >= maxID)
return;
stTmInfo * tinfo = NULL;
map<ID, stTmInfo*>::iterator it = TimerMap.find(TimerID);
if(it != TimerMap.end())
{
tinfo = it->second;
}

time_t tickLast = 0, tickCur = 0;
tickLast = time(NULL);

if(tinfo == NULL)
{
cout << "timer with timerID = " << TimerID << " is not exist!" << endl;
return;
}

while(1)
{
tickCur = time(NULL);
time_t diff = (tickCur - tickLast) * 1000;
//cout << "diff = " << diff << endl;
if(diff >= tinfo->count)
{
(*(tinfo->timerCb))(tinfo->count);
break;
}
}
return;
}

void Timer::DeleteTimer(ID TimerID)
{
if(TimerID < 0 || TimerID >= maxID)
return;

TimerMap[TimerID] = NULL;
TimerIsCreate = 0;
cout << "Delete Timer with TimerID = " << TimerID << " success!!!" << endl;
return;
}

void onTimer(int count)
{
cout << "Timer count: " << count << "ms" << endl;
return;
}

int main()
{
/*Timer*/
Timer tm;
stTmInfo timerInfo;
timerInfo.count = 5000;
timerInfo.timerCb = onTimer;
ID timerID = 0;
tm.CreateTimer(timerID, &timerInfo);
tm.CreateTimer(timerID, &timerInfo);
tm.StartTimer(timerID);
tm.DeleteTimer(timerID);
tm.StartTimer(timerID);

tm.CreateTimer(timerID, &timerInfo);
tm.CreateTimer(2, &timerInfo);
tm.StartTimer(2);

Timer tm2;
tm2.CreateTimer(2, &timerInfo);
tm2.StartTimer(2);

stTmInfo timerInfo2;
timerInfo2.timerCb = NULL;
tm.CreateTimer(3, &timerInfo2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 定时器