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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Mootools 1.2教程 定时器和哈希简介
- C#定时器和随机数
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C#中timer定时器用法实例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例