2

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.

Edward Goodson
  • 302
  • 1
  • 11
  • Could you add the entire source code ? I would like to see the way you initialize "five_seconds", and the call to event_base_dispatch, if possible. – Remi Gacogne Apr 18 '13 at 11:57

1 Answers1

2

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

nos
  • 223,662
  • 58
  • 417
  • 506
  • The link to the docs is most helpful. I have been looking for something like them and haven't found them. I have found less complete sources through google but these are wonderful. I have event_new assigned to toEvent, Am I calling the function wrong. – Edward Goodson Apr 17 '13 at 18:57
  • I think it might be the time interval I accidently left it running when I went to lunch and it had printed out the statement several times when I got back, but it is taking much longer than the 5 seconds I thought I had set it to. – Edward Goodson Apr 17 '13 at 18:59