libevent简单应用:设置定时器

来源:互联网 发布:php argv argc 编辑:程序博客网 时间:2024/04/29 22:09
#include <event.h>
#include <iostream>
using namespace std;


struct event ev; //事件
struct timeval tv; //定时器


/*事件处理函数,cb=callback*/
void time_cb(int fd,short _event,void *argc)
{
cout<<"timer wakeup"<<endl;
event_add(&ev,&tv);/*重新添加定时器*/
}


int main()
{
WSADATA WSAData; /*初始化WINDOWS socket库*/
WSAStartup(0x101, &WSAData);


struct event_base *base = event_init();//初始化libevent库,相当于初始化一个Reactior,就可以注册事件了.
tv.tv_sec=10; //间隔
tv.tv_usec=0;
evtimer_set(&ev,time_cb,NULL);//初始化关注的事件,并设置回调函数
//等价于event_set(&ev, -1, 0, timer_cb, NULL);


event_add(&ev,&tv);//注册事件 相当于调用Reactor::register_handler()函数注册事件
event_base_dispatch(base);//进入消息循环和消息分发






}
原创粉丝点击