#define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg))
#include <cstdio> #include <errno.h> #include <sys/types.h> #include <event.h> #include <event2/event.h> #include <event2/event_struct.h> #include <event2/event-config.h> #include <event2/util.h> void do_timeout(evutil_socket_t fd, short event, void *arg) { printf("do timeout (time: %ld)! ", time(NULL)); } void create_timeout_event(struct event_base *base) { struct event *ev; struct timeval timeout;
//将事件和event_base绑定
ev = event_new(base, -1, EV_PERSIST, do_timeout, NULL); if (ev) { timeout.tv_sec = 5; timeout.tv_usec = 0; event_add(ev, &timeout); } } int main(int argc, char **argv) { struct event_base *base; base = event_base_new(); if (!base) { printf("Error: Create an event base error! "); return -1; } create_timeout_event(base); event_base_dispatch(base); return (0); }