Detecting idleness

maarten maarten.foque at
Tue Apr 6 11:16:58 CEST 2010

Another way may be to use only 1 timer.
Set it to your timeout value (e.g. 30 seconds) and restart it in all
your callbacks.  This way this timer will only run out if no other
callbacks are called.
(Just don't forget to restart it somewhere)

The docs explain this in the timer section, when using timers to monitor
a connection.


On Mon, 2010-04-05 at 16:55 +0200, Marc Lehmann wrote:
> On Sun, Apr 04, 2010 at 11:26:51PM -0700, ry at wrote:
> > Can someone suggest a good way of detecting a period of idleness? That
> > is, a way to get a callback after the event loop has done nothing for
> > N seconds. (I suppose some combination of an ev_idle, ev_timer, and
> > maybe ev_check watchers...)
> I don't see a way to accomplish that with libev alone. Note that
> "idleness" of your app is not the same as "idleness" of libev (libev
> itself might have timers, or other parts of your app).
> What you can do is, each time your app is doing sth. non-idle, save ev_now()
> in a last_activity variable. Alos have a timer runnign every "N" seconds.
> When the timer fires, check last_activity. If last_activity < ev_now ()
> - N, then you are are for at least N seconds. Otherwise just restart the
> timer to fire after last_activity + N - ev_now ().

More information about the libev mailing list