Adding watchers on the fly to event loops

Aravindh Ramaswamy aravindhramu at
Wed Jul 10 13:35:45 CEST 2013

On Wed, Jul 10, 2013 at 4:54 PM, Marc Lehmann <schmorp at> wrote:

> 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.

Correct but that would require synchronization mechanisms between two
threads( as is the case for me) like mutexes and condition variables.
I considered the use of the queue but since watchers can pass custom data
as well
by type casting, I thought of passing the data using ev_async watcher in
and then the recipient event loop while invoking the async_cb can make use
of that information.
Wont that work ?

> 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
>       -----==-     _GNU_    
>       ----==-- _       generation
>       ---==---(_)__  __ ____  __      Marc Lehmann
>       --==---/ / _ \/ // /\ \/ /      schmorp at
>       -=====/_/_//_/\_,_/ /_/\_\

Thanks and Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the libev mailing list