#include <stdio.h>
#include <iostream>
#include <event2/event.h>
// 定时回调函数
void timeoout(int sock, short event, void *arg)
{
std::cout << "timeout!!!" << std::endl;
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
evtimer_add((struct event*)arg, &tv);
}
int main()
{
// 初始化
struct event_base *base = event_base_new();
struct event *timerEvent = evtimer_new(base,timeoout,event_self_cbarg());
// 定时1秒
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
// 添加定时事件
evtimer_add(timerEvent, &tv);
// 事件循环
event_base_dispatch(base);
evtimer_del(timerEvent);
event_base_free(base);
return 0;
}
原文链接:https://blog.csdn.net/caoshangpa/article/details/79308317