gdk-lock sharing between threads



Hi,

I'm having trouble understanding the way gtk/gdk locking works in a
multi-threaded application. According to GTK documentation, to make gtk
calls thread-safe I have to initialize both the GLib and GDK thread
subsytems and enclose the gtk_main() statement in a
gdk_threads_enter()/gdk_threads_leave() pair. Then any calls, including
signal handlers will be thread safe. On the other hand, if I had another
thread trying to perform GTK/GDK calls, I need to enclose them in a
gdk_threads_enter()/gdk_threads_leave() pair.

Question 1a: Does this mean, that a gtk_main() loop, that has been started
in a thread-safe manner within a gdk_threads_enter()/gdk_threads_leave()
pair, actually releases the GDK lock so that other threads may acquire it
and perform GTK/GDK calls?

Question 1b: If so, then how can I make it so, that the GTK main loop would
first perform all the drawing and release the GTK/GDK lock and only then
start entering signal handlers? Or maybe I could have two threads: one
performing the drawing and the other servicing all the callbacks and
handlers?

If so, then if I have two threads, one of which contains the GTK main loop,
and an expose-event handler, and the other has some means to generate data
for the expose-handler to use, then, I need to acquire a GMutex lock in both
threads: when I'm writing new data in the generator-thread, and when I'm
reading data while drawing in the expose-event handler thread. Now If i were
to use any GTK/GDK calls in the generator thread, and would try to acquire a
lock using gdk_threads_enter, the program would enter a deadlock: the main
thread, still having a GTK/GDK lock would enter the expose-event handler and
wait for a lock on the Data, while the generator thread would wait for a
lock on GTK/GDK while still having a lock on the Data.

Question 2: How should I design my thread, so that I could have MULTIPLE
threads locking the GTK/GDK subsystems using gdk_threads_enter() and locking
some arbitrary data using mutexes and not enter a deadlock?

I feel that I'm not making my self clear, but if the term `heisenbugs` is
used to for multi-threading related bugs, then `heisenquestions` are
definitely the nonsense above...

Any ideas would be most appreciated.

solar



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