libevent中的Timer使用
#include <stdio.h> #include <iostream> // libevent头文件 #include <event.h> #include "tool.h" #include "header.h" static struct event tickEvt; static struct event_base *baseEvt; void PrintTime() { struct timeval timer; gettimeofday(&timer, NULL); volatile uint current_time = (uint)(timer.tv_sec); PRINT("current_time:%d", current_time); } // 定时事件回调函数 void onTime(int sock, short event, void *arg) { PrintTime(); struct timeval tv = {.tv_sec = 1, .tv_usec = 0 }; // 重新添加定时事件(定时事件触发后默认自动删除) event_add((struct event*)arg, &tv); } void startEvt() { // 初始化 event_init(); struct event evTime; // 设置定时事件 evtimer_set(&evTime, onTime, &evTime); struct timeval tv = {.tv_sec = 1, .tv_usec = 0 }; // 添加定时事件 event_add(&evTime, &tv); // 事件循环 event_dispatch(); } void StartEvent() { // 初始化 //event_init(); baseEvt = event_base_new(); // 设置定时事件 evtimer_set(&tickEvt, onTime, &tickEvt); struct timeval tv = {.tv_sec = 1, .tv_usec = 0 }; // 添加定时事件 //event_add(&tickEvt, &tv); event_base_set(baseEvt, &tickEvt); evtimer_add(&tickEvt, &tv); // 事件循环 //event_dispatch(); event_base_loop(baseEvt, 0); } int main(int argc, char* argv[]) { //startEvt(); StartEvent(); return 0; }
对libevent中Timer的分析:(http://www.monkey.org/~provos/libevent/doxygen-2.0.1/)
Timer:
libevent can also be used to create timers that invoke a callback after a certain amount of time has expired. The evtimer_set() function prepares an event struct to be used as a timer. To activate the timer, call evtimer_add(). Timers can be deactivated by calling evtimer_del().
大意思是:
libevent也能被用作一个“定时触发器”,当指定的时间到期时,能够回调指定的特定函数(比如上面的代码是回调event_handler,也就是说event_handler每个1秒就再回调一次自己)。evtimer_set()函数初始化一个timer,激活timer调用evtimer_add()函数,删除timer调用evtimer_del()函数。
1.evtimer_set()函数原型:
void evtimer_set(struct event ev, void(*)(evutil_socket_t, short, void *) cb, void *arg)
该函数修改结构体ev,将回调函数cb,回调函数的参数arg赋值该结构体ev中的成员。
也许看到这里你会有一个疑问:这是因为:
#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
2.event_base_set函数原型:
int event_base_set(struct event_base *eb, struct event *ev)
Associate a different event base with an event.
将一个event关联到一个不同的event base. 也就说我们可以将一个event关联到不同的event base.
3.evtimer_add函数原型:
int evtimer_add(struct event *ev, const struct timeval *timeout)
Add an event to the set of monitored events. 将一个event加入到被监视的事件集合。
ev an event struct initialized via event_set()
timeout the maximum amount of time to wait for the event, or NULL to wait forever.
4.evtimer_del函数原型:
#define evtimer_del(ev) event_del(ev)
so:
int evtimer_del(struct event * ev)
Delete a timer event.
ev the event struct to be disabled
总结一下:
1. 首先调用event_base_new(或者event_init,推荐调用前者),对libevent进行唯一的一次初始化。
before using any of the functions in the library, you must call event_init() or event_base_new() to perform one-time initialization of the libevent library.
2. 然后调用evtimer_set初始化一个struct event结构体,也就是将利用evtimer_set的参数(即回调函数和回调函数的参数)填充struct event 结构体。
The evtimer_set() function prepares an event struct to be used as a timer。
再调用evtimer_add()将被evtimer_set初始化的struct event结构体加入到被监视的集合中.
To activate the timer, call evtimer_add().
删除一个event调用evtimer_del()
Timers can be deactivated by calling evtimer_del().
3.最后调用event_base_loop()或者event_base_dispatch()或者event_dispatch(),推荐调用第一个,最后一个是非线程安全的版本。
从而开始处理事件集合,开始对event集合进行监视monitor.
Loop to process events. In order to process events, an application needs to call event_dispatch(). event_base_dispatch() is threadsafe event dispatching loop.
event_base_loop() is a more flexible version of event_base_dispatch().
注意带有base字样的函数是对全局的libevent进行操作的。