📄 timercache.h
字号:
#ifndef _TIMER_CACHE_H#define _TIMER_CACHE_H#include <pthread.h>class ITimer;// timer node.typedef struct tag_Timer{ struct tag_Timer * next; // common interface for the client application. ITimer * timer; // the identity id for the timer. int timerId; // false: this is not a periodical timer. // true: this is a periodical timer. bool isPeriodical; // the start time of the timer. int startTime; // the timeout for the timer (second). int interval; // when the timer timeouts, this argument will be with // the timeout callback function. void * args; tag_Timer() { timer = 0; timerId = -1; next = 0; isPeriodical = false; startTime = -1; interval = -1; args = 0; }}Timer;// timer array.typedef struct tag_TimerList{ Timer * nextTimer; tag_TimerList() { nextTimer = 0; }}TimerList;class TimerCache{public: TimerCache(); virtual ~TimerCache(); // insert a timer into the hash table. int insert(ITimer * timer , int currentTime , int interval , bool isPeriodical = false , void * args = 0); // remove the timer from the hash table with the timer id. int remove(int timerId); // when the timer is expired, this function will be called. void handleTimeout(int currentTime); private: // the granularity of the hash table. static const int LIST_CAPACITY = 50; // the timer array. TimerList timerList_m[LIST_CAPACITY]; // the lock for the cache accessing. pthread_mutex_t mutex_m; // if the timer is periodical, after timeout, the timer will be re-inserted into the // hash table. void reInsert(Timer * timer , int currentTime); // reclaim the hash table. void destroyAll(); // get the timer id int getTimerId(); // when the timer is expired, this function will be called. void expire(int currentTime);};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -