Re: Proposal: Enable threads by default



On Sun, 2009-12-06 at 14:22 -0500, Dan Winship wrote:
> On 12/06/2009 10:05 AM, Adam Goode wrote:
> > I guess what I really would like is a way that I can use GLib
> privately
> > from my library and have it be thread-safe without requiring users
> of my
> > library have to link against gthread and call g_thread_init. I think
> it
> > is primarily just GSlice that is the trouble here
> 
> Currently, everything in libglib (including GSlice) can deal with you
> calling g_thread_init() at any time, although the documentation pretty
> much explicitly claims otherwise. Maybe we should change the rules
> around g_thread_init as part of this:
> 
>     1) If you want to call g_mem_set_vtable(), it must be the very
> first
>        glib call in the program. (This rule already exists, although
>        the current g_thread_init() docs contradict it.)
> 
>     2) If you want to call g_thread_init() *with a non-NULL
> parameter*,
>        you have to call it before any other glib method except
>        g_mem_set_vtable(). (This is the current g_thread_init rule,
>        weakened to only apply to the uncommon case.)
> 
>     3) You can call g_thread_init(NULL) at any time, and everything in
>        libglib will deal with it. (Already true, but undocumented.)
>        Libraries at higher levels of the stack might not work
> correctly
>        though if you call g_thread_init() after calling into that
>        library. (Already true and documented.)

This is not strictly true. You must not call g_thread_init() from a
callback from glib or any place where it may be holding a lock.

Additionally, the support for this in libglib relies on the fact that
everything up to (and including) the g_thread_init() call that uses glib
is done on the main thread. You can't call any glib function on another
thread before threads are initialized.

>     4) Calling g_thread_init(NULL) after g_thread_init() has already
>        been called once (with a NULL or non-NULL param) is a no-op.
>        (We don't actually want this rule, but it avoids problems with
>        apps that were mistakenly calling g_thread_init() after
>        g_type_init() before.)
> 
>     5) g_type_init() calls g_thread_init(NULL)



-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Alexander Larsson                                            Red Hat, Inc 
       alexl redhat com            alexander larsson gmail com 
He's a globe-trotting chivalrous filmmaker fleeing from a secret government 
programme. She's a blind Bolivian mermaid looking for love in all the wrong 
places. They fight crime! 



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