Search code examples
libevent

What are my errors using libevent timout events?


I am trying to learn libevent for use in a future project. I am attempting to create a event that calls it's call back function each time it times out. All the call back function does is print "timeout_cb called" to standard out.

my code for the call back function is:

static void timeout_cb(evutil_socket_t fd, short what, void *arg) { 
printf("timeout_cb called");
}

my code for the event is:

struct event *toEvent; // time out event do this every so often

toEvent = event_new(base, -1, EV_TIMEOUT, timeout_cb, NULL); // base is the event base
event_add(toEvent, &five_seconds); //five_seconds is a timeval struct with 5 seconds 

The program will compile and run with no errors or warnings but it doesn't print out the phrase in the call back function. I have used similar printf statements in other callback types to verify they were called and that various lines were reached inside the functions, but this does nothing. I waited 30 seconds or so but still nothing printed to the screen. What am I doing wrong the with the pure timeout event.


Solution

  • You'll have to do

    event_new(base, -1, 0, timeout_cb, NULL);
    

    Note that there's some convenience macros to add timers, evtimer_new(), evtimer_add(), see the docs