ev_async_send() not trigerring corresponding async handler in target loop ?

Marc Lehmann schmorp at schmorp.de
Tue Dec 29 14:07:56 CET 2009

On Tue, Dec 29, 2009 at 01:20:53PM +0100, Pierre-Yves Kerembellec <py.kerembellec at gmail.com> wrote:
> I'm quite new to libev and I'm having a hard time figuring out why a call to ev_async_send() will not trigger the corresponding async handler
> in the specified target loop.

Any watcher, including an async watcher, must be started first. It will
likely work much better if you add an appropriate.

   ev_async_start (loop, &w);

> As you can see, the round-robin distribution to worker threads seems to be fine, but the async_cb is never called. Despite the fact that
> I need to add a timer watcher to each worker thread loop (to keep in alive in absence of any other), I've been digging a little but into the

Adding a timer is a possibility, another is to call ev_ref (loop).

> libev code and it seems the internal evpipe_init() function (responsible for creating and initializing a communication eventfd/pipe
> watcher is actually never sent. ev_async_start() on the other end will call evpipe_init(), but my understanding is that it's not thread-safe

Calling evpipe_init is indeed the job of ev_async_start.

> (because it's not using the communication pipe and changing directly the loop  internal async table from another thread).

You have to start the watcher in the thread that waits for it.

> Am i missing something here ? Am I using the right workflow ?

Probably not - start the ev_async watcher in the thread that runs the
loop, then use ev_async_send from other threads.

Whether you then use one async watcher per thread or just one globally (or
something else) is then a matter of design.

> For now, I'm back at using my own pipes between the main and the worker threads, adding an ev_io watcher to the reading side of each
> pipe, and writing accepted network handles to the pipes in a round-robin way (like the above). I actually mimic the ev_async_send()
> behavior, and it works quite fine. But I'd really like to use the provided libev async facility if possible, instead of re-inventing the wheel
> in my code.

Indeed, it will also likely be faster.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at goof.com
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list