Is that ev_run()/ev_loop() inhibited in multiple thread of current libev?

Marc Lehmann schmorp at
Wed Dec 21 20:31:00 CET 2011

On Thu, Dec 22, 2011 at 01:58:52AM +0800, freebsdj <freebsdj at> wrote:
> My project is a networking server, I have an assign thread to accept
> connections and
> dispatch fd to query threads, both assign thread and query threads are
> using ev_run()

First, can you properly format your e-mail? It's really
annoying and
tiresome to read read text that constantly
the line lengths it uses.

> The issue is that it will crash while calling ev_run() in query thread,
> with the message:

Well, stupid question, are you sure you don't use the same loop in both
calls to ev_run, without any mutex?

> I noticed that the description in manual: better to create a new ev_loop in
> every thread, but according
> to my experiences of using libev in 2009, I can do that.

You don't need to create a new loop in every thread, as long as you use
locking. In general, when using threads, you have to use mutexes or other
means to protect shared resources. The acquire/release callbacks can help
you do that.

It is probably far easier to use a loop per thread though, as no locking
is required on your part then.

> 1. Can I use ev_loop() in multi-threads environment(though not preferred)?

No, libev is certainly preferred in multi-threaded environments :)

> 2. Why check the loop->loop_done there?

To catch bugs like the one in your code as early as reasonably possible
(some assertions exist to check new data structures or code changes, and
normally get removed over time). Asserts are meant to be a debugging help:
for example, most functions in libev use assert to do basic parameter and
data structure checking.

I'd guess the parameter checking is very helpful when developing programs,
and could easily be left on in production I presume. I certainly donät
bother switching off assertions in most of my code.

> 3. If I compiled with -DNDEBUG, all the assert()s are ignored, so it will
> not crash, but it doesn't seems work
> correctly. Is that an inappropriate design to put code in assertions?

The rules of C require code to be used in assertions. I suspect other
languages do, too. Maybe you need to rephrase your question?

If you mean that errors are no longer checked via assert when compiling
with NDEBUG, then, well, yes, thats the whole point of NDEBUG, to disable

You can enable extra verification via EV_VERIFY - the assertions in libev
are only there to avoid the worst, and are always low-overhead, while
EV_VERIFY can result in considerable performance loss.

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

More information about the libev mailing list