libevent 定时器
过程如下图:
1 evtimer_new 创建定时器事件,设置回调。
2 evtimer_add 改变定时器状态为pending,设置定时器时间。
代码如下:
void TimerCallBack(evutil_socket_t, short, void* val) {
struct timeval tv = {2, 0};
evtimer_add(*((struct event**)val), &tv);
}
int main()
{
struct event_base* base = NULL;
struct event* timer = NULL;
struct timeval tv = {2, 0};
base = event_base_new();
if (!base)
return -1;
timer = evtimer_new(base, TimerCallBack, &timer);
evtimer_add(timer, &tv);
event_base_dispatch(base);
return 0;
}