best ways to do clean up

Marc Lehmann schmorp at schmorp.de
Wed Dec 29 03:54:02 CET 2010


On Tue, Dec 28, 2010 at 06:46:25PM -0400, Juan Pablo L <jpablolorenzetti at gmail.com> wrote:
> What i m trying to do is a custom watcher, in glib you override the
> check/prepare/dispatch and finilize so when the loop in glib is exiting
> the loop itself would call the finalize function on all event sources
> (watches in libev) automatically

Sounds like a job for an ev_cleanup watcher, maybe, depending on what you
mean with "loop exiting".

in most cases, however, you can just call the finaliser functions you want
yourself after exiting the loop.

> but in libev you do not get to
> customize the check/prepare/dispatch/finalize functions

libev is actually much more customisable than glib in that respect, as you
can build your own watchers from components (see ASSOCIATING CUSTOM DATA
WITH A WATCHER in the docs, which also shows how to combine two functions
into one custom watcher).

glib doesn't allow for much customisation - you get one type of watcher,
and that's it.

> (although you can modify prepare and check but at the loop level and not
> at the watcher level)

check and prepare are at the loop level as well in glib - glib calls each
single watcher (multiple times, even), while in libev, you can choose for
which watchers this overhead is done, otherwise they work more or less
identical (the API is different so libev can take advantage of advanced
kernel interfaces, which glib cannot really do).

my guess is that as soon as you have internalised this (and learned how
to do it in practise :), you will be as comfortable with libev as you
were with glib - and many more event loops, as most event loops work like
libev, not like glib (for performance reasons).

> so i m not sure what is the correct way to do this in libev, i saw a
> ev_cleanup watcher , so the libev solution for cleaning up some memory used
> for other watchers at the end of the loop
> would be to register a watcher per watcher-to-clean so they do the cleaning
> at the end?

possibly - a lot depends on what you mean with cleanup.

> just a clarification, i m making a server and i create a new
> thread per connection and each thread has
> its own loop so i can not leave the OS clean the memory as one loop
> destroying does not mean the application is exiting, it means only one
> client disconnecting. thanks!!!

well, I'd say the solution to that is to just free all your data when
ev_run returns/when your thread is about to end :) ev_cleanup can help
with that, but libev doesn't manage your resources for you, you have to do
that yourself.

If the overhead doesn't concern you (it's still much lower than doing the
equivalent with glib) you can simply add an ev_cleanup to each of your
custom watcher structures and start it when the watcher is started.

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



More information about the libev mailing list