Reactor模式
:主线程只监视是否有客户端链接,创建一个新线程来进行其他服务
使用该库的代码
编译时需指定库 -levent
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include<event.h>
#include<signal.h>
void sig_fun(int fd,short ev,void* arg)
{
printf("sig=%d\n",fd);
}
void time_cb(int fd,short ev,void* arg)
{
if(ev & EV_TIMEOUT)
{
printf("time out\n");
}
}
int main()
{
struct even_tbase* base=event_init();
assert(base!=NULL);
struct event*sig_ev=evsignal_new(base,SIGINT,sig_fun,NULL);
event_add(sig_ev,NULL);添加事件,无超时时间设置
struct timeval tv={5,0};
struct event* time_ev=evtimer_new(base,time_cb,NULL);定时器
event_add(time_ev,&tv);
event_base_dispatch(base);
event_free(sig_ev);
event_free(time_ev);
event_base_free(base);
}