Adding watchers on the fly to event loops

Marc Lehmann schmorp at
Wed Jul 10 13:24:52 CEST 2013

On Wed, Jul 10, 2013 at 04:44:25PM +0530, Aravindh Ramaswamy <aravindhramu at> wrote:
> > You can add watchers at any time when libev itself isn't executing. For
> > example, from inside an ev_async callback (but why would you need ev_ref
> > when
> > you already have an ev_async watcher registered?).
> >
> Right. A mistake from my end. I didnt have EV_ASYNC in mind initially.
> My doubt was should i use ev_suspend and ev_resume to do so.

No, those two functions are there to freeze the event loop (for exmaple,
when you receive a SIGTSTP).

The cleanest design is to have some queue where you add new
requests/watchers/whatever you want to do, and then use ev_async to wake
up the event loop. The ev_async callback can then munch through the queue
and do whatever is needed, for example, you could put new file descriptors
into the queue, and ev_async then registres ev_io watchers for it, or
something similar.

You only need the loop locking when you want to add watchers without direct
libev support. But even in that case you need an ev_async watcher, and the
extra locking might not be worth it.

In my experience, not locking libev at all, and doing a separate queue
(with locking...) as outlined before, is best.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list