On Tue, Sep 09, 2008 at 04:48:40PM -0500, Andrew Troschinetz wrote: > Hi all, > > I'm just getting started with libevent and I was wondering how I might > implement a simple deadline timer with it. > > I'd like to be able to use do something like: > > void do_something(...) ... > deadline_timer_wait(5, do_something); /* _non-blocking_, call > do_something() in 5 seconds */ > ... perhaps less than 5 seconds later ... > deadline_timer_nevermind(); /* whatever we're waiting on, stop waiting, > don't call do_something() */ > > > However I'm a bit confused as to how event_dispatch() is supposed to work. > Do I need to fork and then have the child call event_dispatch() in a loop:
Definitely not. In this simple case, perform the initial timeout_set() and timeout_add(). Then call event_dispatch(). When the callback is called, do_something() in this case, do what you need to do, then re-add the timeout event if you want it to fire again. In the nevermind case, use timeout_del() to remove the timer. The callback for it will then never be called. However, please be sure to remember that if you have no events for libevent to handle (e.g. none added, or all removed), event_dispatch() will return. This means if you remove the timer from the event queue, and there are no other events being handled, event_dispatch() will definitely return. In an event loop paradigm like this, it is standard procedure to structure things as if you were inside of event_dispatch(), with the assumption that it will return only when your application is exiting. This means using things like queues, interval timers, etc. to make sure work is done. At first it may seem foreign, but remember that if one doesn't have anything to actually wait for, there's no reason for an event loop to even exist. In your case, what you want to wait for is the timer's consistent firing. In the case of cancellation, you're then either waiting for input from some other source to rearm this timer or you're exiting the application. -cl _______________________________________________ Libevent-users mailing list Libevent-users@monkey.org http://monkeymail.org/mailman/listinfo/libevent-users