glib/gdk/gtk thread safety during startup



this is *not* a question about using threads with glib in the normal
sense. i've read the FAQ, and i understand the basic idea.

i can't initialize GTK from my main thread - my main thread is not the
thread that communicates with the X server. Once the GTK thread gets
running, it is the only thread that ever communicates with the X
server (i.e. its the only thread that uses any Gtk--, GTK+ or glib
functions), which ought to prevent thread-related problems. But it
does not. Instead, the only way to prevent the problem seems to ensure
that the glib/gdk/gtk libraries are fully loaded and initialized
before any pthread is created.

there is a very, very good inverse correlation to the likelihood of
all of the GTK libs being memory-resident, and the chance of a
random-ish core dump. if its likely that loading the GTK libraries
will take a while, and my program will create a pthread in the
meantime, then i am almost certain to crash.

Note: using g_thread_init() does not help.

right now, i try to enforce this by explicitly calling gtk_init() when
there is only one thread. but even this does not seem to be enough.
the only guaranteed way to stop the problem is to prevent any other
thread from being created until i know for sure that all (most? a
lot?) of GTK is linked in.  that is, if all i do is to call gtk_init()
before any more threads are created, i will sometimes crash. if i
instead block all other thread creation until the GTK thread has
mostly finished its initial setup (calling a bunch of GTK stuff that
presumably can cause more run-time linking and function calls to be
done/made), then i never crash.

if these other threads used glib or gdk or gtk, i could understand
this. but they do not! not one little call!

i will gladly spend more time tracking this down, but first i want to
know if anyone knows more about it than i do. it basically seems that
there is one or more function calls made by GTK (probably to glib)
that cause problems if the application is multithreaded *at that point
in time*. that is, they expect to be called before other pthreads have
been created.

i suspect that most multi-threaded programs that use GTK use the main
thread as the GTK thread. this will work without problems, since they
are likely to call gtk_init() and whatever it is that calls the bad
functions before creating other threads. certainly i have programs that
work this way, and they do not ever crash in the same way. 

--p

ps. nice to be back on gtk-list after several months away.



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