4、my_timer.h
#ifndef MY_TIMER_H #define MY_TIMER_H #define LIMIT_TIMER 1 //有限次数定时器 #define CYCLE_TIMER 2 //循环定时器 extern "C" { #include "minheap-internal.h" } class Timer { public: Timer(); virtual ~Timer(); /************************************** * input: interval: 每次执行的时间隔间, 单位是毫秒。 * fun arg : 回调函数以及参数。 * flag : 循环定时器还是有限次数定时器,如果是相对定时器 * exe_num : 只有在有限次数定时器才有效,表示执行的次数。最少为1次 * return: 生成定时器的ID **************************************/ unsigned int timer_add(int interval, void (*fun)(void*), void *arg, int flag = CYCLE_TIMER, int exe_num = 0); /*************************************** * description: * 去掉已经加入的定时器,比如产生定时器的母体已经消亡了,在消亡之间要将其删除。 * 相对定时器在任务完成后会Timer会自己释放掉。 ***************************************/ bool timer_remove(unsigned int timer_id); /*************************************** * description: Timer属于被动对象,没有自己的执行线程,属于被调用者。这样主要是为了避免产生线程同步。 * 定时器的循环处理函数,由定时器的拥有者进行循环调用。它的最小时间间隔决定了定时器的精度。 ***************************************/ int timer_process(); private: struct min_heap _min_heap; unsigned int _timer_id; }; #endif // MY_TIMER_H
5、my_timer.cpp
#include "my_timer.h" Timer::Timer() : _timer_id(0) { min_heap_ctor_(&_min_heap); } Timer::~Timer() { for (int i = 0; i < _min_heap.n; i++) { free(_min_heap.p[i]); } min_heap_dtor_(&_min_heap); } unsigned int Timer::timer_add(int interval, void(*fun)(void*), void *arg, int flag /* = CYCLE_TIMER */, int exe_num /* = 0 */) { struct event * ev = (struct event*) malloc(sizeof(struct event)); min_heap_elem_init_(ev); if (NULL == ev) return NULL; struct timeval now; gettime(&now); ev->ev_interval.tv_sec = interval / 1000; ev->ev_interval.tv_usec = (interval % 1000) * 1000; evutil_timeradd(&now, &(ev->ev_interval), &(ev->ev_timeout)); ev->ev_flags = flag; ev->ev_callback = fun; ev->ev_arg = arg; ev->ev_exe_num = exe_num; ev->timer_id = _timer_id++; min_heap_push_(&_min_heap, ev); return ev->timer_id; } bool Timer::timer_remove(unsigned int timer_id) { for (int i = 0; i < _min_heap.n; i++) { if (timer_id == _min_heap.p[i]->timer_id) { struct event * e = _min_heap.p[i]; min_heap_erase_(&_min_heap, _min_heap.p[i]); free(e); return true; } } return false; } int Timer::timer_process() { struct event *event; struct timeval now; while ((event = min_heap_top_(&_min_heap)) != NULL) { gettime(&now); if (evutil_timercmp(&now, &(event->ev_timeout), < )) break; min_heap_pop_(&_min_heap); event->ev_callback(event->ev_arg); if (event->ev_flags == CYCLE_TIMER || (event->ev_flags == LIMIT_TIMER && --event->ev_exe_num > 0)) { evutil_timeradd(&(event->ev_timeout), &(event->ev_interval), &(event->ev_timeout)); min_heap_push_(&_min_heap, event); } else { free(event); } } return 0; }
6、main.cpp
#include <iostream> #include "my_timer.h" #include <unistd.h> using namespace std; static void fun(void *arg) { int *id = (int *) arg; cout << *id << endl; } int main() { cout << "Hello World!" << endl; Timer t; int id1 = 100; t.timer_add(1000, fun, &id1);//ms int id2 = 101; t.timer_add(5000, fun, &id2); int id3 = 102; t.timer_add(3000, fun, &id3); while (true) { t.timer_process(); sleep(1); } system("pause"); return 0; }