best ways to do clean up

Marc Lehmann schmorp at
Wed Dec 29 16:31:34 CET 2010

On Tue, Dec 28, 2010 at 11:19:58PM -0400, Juan Pablo L <jpablolorenzetti at> wrote:
> be very efficient and useful but since i have no experience with other event
> library other than glib i can not stop my self from trying to do same with
> libev.

The design of glib's mainloop is very interesting, unfortunately it precludes
any kind of optimisation, each loop run the loop has to query each watcher

In a sense, glib doesn't actually offer support for event sources, each
watcher has to implement them on their own. This makes it's watchers very
generic, and is a remarkably different design to basically any other event

This is probably quite fine in an environment where you want to watch at
most one fd and half a dozen timers (for which glib was designed).

When you don't want to pay this overhead, then you need to tell the event
loop in advance about the event sources you are interested in, which is
why most event loop designs ask you to register interest in specific
events, as opposed to registering some object with behaviour.

> since i m customizing an ev_async, in glib you create a custom watcher and
> you can override the prepare and check functions (also the finalize
> function) so when the loop loops it calls your prepare and check functions
> for only this custom watcher without affecting the prepare/check/finalize
> functions that may be in the same loop, is this possible with libev that is,
> to customize the prepare and check functions only for particular watcher
> without affecting the other watchers in the loop ??? i ask this because i
> find this to be very useful to me.

I am sure I don't quite understand your problem, because it really is
the same way in libev. There is no such thing as per-loop prepare/check
functions - the only way to get these events is to register watchers for
them, and then only those watchers which have been started will receive
these events.

So basically, you register an ev_prepare and ev_check watcher, and then
your watcher will be called before and after polling for events.

Note, however, that prepare/check are almost always the wrong way to do
things, they are only useful if you want to include events that libev
cannot handle itself (for example, because the software you use doesn't
give you enough information to create watchers yourself).

The reason they are usually wrong is that they are called each time the event
loop polls for events. This is the only way to do things with glib, which is
why it is the right thing there, but from a performance standpoint, you have
to avoid it when possible.

Maybe you should describe your actual goal, i.e. what are you tring
to achieve, instead of trying to achieve your goal with something
(check/prepare) that might not be the right tool in the first place?

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

More information about the libev mailing list