On Fri, Jun 3, 2011 at 2:34 AM, Marc Lehmann <[email protected]> wrote: > Why would anybody want to use specifically a 0-second timer (where the 0 > isn't just the chance result of some calculation)? > > The only reason I can know from past experience is that some people try > to abuse 0-second timers as some replacement for idle watchers, possibly > because that hack was the only way to get some kind of idle behaviour with > some other event loop.
No, it's not about idle at all. Imagine a massively concurrent system where most coroutines are handling the network, but some occasionally need to do a rather long/complex computation. If those coroutines don't want to block other coroutines from doing anything useful, they need to yield as frequently as they can, and convenient way to do it is using sleep(0). Idle wouldn't do a job here, because if other coroutines are busy with network or timers or anything else, idle callbacks won't be called and computation will halt. Also, ev_check won't work here, because on the next iteration this callback will be called first, it might not have given other coroutines a chance to work yet, besides as far as I understand ev_check doesn't influence poll timeout. Immediate timers seem to be the only primitive that has necessary properties: - needs to make sure backend_poll() is called - needs to make sure any pending io is processed - needs to make sure backend_poll() doesn't sleep - needs to make sure it is called as soon as possible after io is processed and preferable in order fair to others Currently ev_timer with timeout of 0.0, sadly, doesn't satisfy all these properties. However, ev_timer with timeout of -1.0, or any big enough negative timeout, does. P.S. Of course the proper way to handle these computations is to use a thread pool and offload computation tasks there. It's actually what I ended up doing in one of my systems and it works a lot better than yielding. _______________________________________________ libev mailing list [email protected] http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev
