Re: g_threads_enter() equivalent



On Fri, 7 May 2010 13:12:29 +0200 (CEST)
Mark Roberts <gtkmm manumark de> wrote:
> > On Thu, 2010-05-06 at 16:20 -0400, Talguy wrote: 
> >> I was wondering if there is an equivalent function to
> >> g_threads_enter()/leave() in gtkmm.  Is gtkmm thread aware like
> >> gtk+ is.  Reason why I would like to know is I want to port the
> >> multi threaded animation example of cairo's site to gtkmm to
> >> better help me out in my animations.
> >> 
> >> http://cairographics.org/threaded_animation_with_cairo/
> >
> > There are not equivalent functions for those in gtkmm.  However,
> > there are thread capabilities in glibmm that might be used to
> > achieve similar results (though from posts on this list using
> > threads in gtkmm might not be very straightforward).  There are
> > thread examples that can be used as reference points:
> >
> > http://library.gnome.org/devel/glibmm/stable/examples.html 
> >
> > -- 
> > José
> 
> The C++ way with pairs of functions like g_threads_enter() and 
> g_threads_leave() is using objects of utility classes like 
> Glib::Mutex::Lock. The constructor locks, the destructor unlocks.
> This makes your program exception safe, because the destructor will
> not be forgotten.
> 
> There is a list of thread related utility classes here:
> 
> http://library.gnome.org/devel/glibmm/unstable/group__Threads.html
> 
> There is an example that uses Glib::Mutex::Lock here:
> 
> http://library.gnome.org/devel/glibmm/stable/thread_2thread_8cc-example.html
> 
> Hope this helps,

This is wrong.  gdk_threads_enter() and gdk_threads_leave() lock and
unlock the GDK global lock.  You cannot replace that with a mutex of
your own, because you cannot control what happens in main loop events
with such a mutex.  You have to use the lock which GDK uses,
namely its global lock.

In theory you can use g_threads_enter() and g_threads_leave() with
gtkmm, but it won't do you much good because libsigc++ is not thread
safe.

The best approach is to use Glib::Dispatcher, and not to derive any
class from sigc::trackable which might have more than one thread create
or operate on slots representing any of its methods.  You have to be
very careful when writing multi-threaded programs using gtkmm however.

There is copious past mailing list threads on this in the archives
which the OP can read.

Chris


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]