#include<event2/event.h>/* Here's a callback function that calls loopbreak */voidcb(int sock,short what,void*arg){structevent_base*base= arg;event_base_loopbreak(base);}voidmain_loop(structevent_base*base,evutil_socket_t watchdog_fd){structevent*watchdog_event; /* Construct a new event to trigger whenever there are any bytes to read from a watchdog socket. When that happens, we'll call the cb function, which will make the loop exit immediately without running any other active events at all. */ watchdog_event =event_new(base, watchdog_fd, EV_READ, cb, base);event_add(watchdog_event,NULL);event_base_dispatch(base);}
实例2:执行事件循环10秒,然后退出
#include<event2/event.h>voidrun_base_with_ticks(structevent_base*base){structtimevalten_sec;ten_sec.tv_sec =10;ten_sec.tv_usec =0; /* Now we run the event_base for a series of 10-second intervals, printing "Tick" after each. For a much better way to implement a 10-second timer, see the section below about persistent timer events. */while (1) { /* This schedules an exit ten seconds from now. */event_base_loopexit(base,&ten_sec);event_base_dispatch(base);puts("Tick"); }}