#define DEFAULT_INTERVAL 1
int TIMER_CNT = 0;
class Timer; //定时器
class TimerManager; //定时器管理器
class TimerManager {
friend class Timer; //友元类,Timer类可分享此类的方法
public:
typedef enum {
TIMER_MANAGER_STOP=0,
TIMER_MANAGER_START
}TimerManagerState;
static TimerManager *instance(); //当前实例
void start(); //启动当前线程,运行process函数
void stop(); //终止当前线程
void dump(); //清理当前剩下的无用定时器
void add_timer(Timer *vtimer); //线程安全的增加定时器
void remove_timer(Timer *vtimer); //线程安全的移除定时器
protected:
static void *process(void *);
private:
TimerManager();
void add_timer_unsafe(Timer *vtimer); //线程非安全的增加定时器,本类使用
void remove_timer_unsafe(Timer *vtimer);//线程非安全的移除定时器,本类使用
static TimerManager *m_instance;
static pthread_mutex_t workmutex;
TimerManagerState m_state;
LIST_HEAD(,Timer) list_; //链表头
static int mark;
};
class Timer {
friend class TimerManager;
public:
typedef enum {
TIMER_IDLE = 0,
TIMER_ALIVE,
TIMER_TIMEOUT