gtk and threads (was Re: Strange quirk)



Rich Neitzel wrote:
> Jeff Garzik wrote:
> >What exactly is the thread doing?

> An infinite loop - wait on either a read() call for the fifo case or a
> msgrcv() call for the queue case, then massage & draw the data.

> >Was Glib compiled with _REENTRANT also?

> Sorry, I thought was implied in saying gtk 1.0.6 was, since gtk, gdk &
> glib use the same set of CFLAGS.

It is, I just missed the "1.0.x" part.  :)  I'm so used to thinking in
terms of gtk 1.1, which glib is a separate entity.


> I should also mention that I call XInitThreads() as the 1st action in
> main(). Further, I have tried calling XLockDisplay()/XUnlockDisplay()
> before/after calling any gtk/gdk functions in the drawing thread. I
> have also tried not using them, but the results are the same.
> 
> Further, there is no need to have data flowing to cause the
> problem. Pressing any key in an entry at any point results in all
> threads blocking.

That's a nutty problem.  I can only offer general advice, since I'm
doing much the same stuff as you are.

Based on advice from this list and my own experiences with Motif, it
seems like making multi-threaded calls to gtk functions is a bad idea. 
Glib especially shares global stuff like allocators and free lists, so
using any glib, gdk, or gtk call is likely to have unintended,
non-reentrant consequences.

My own code keeps all gdk/gtk/glib calls isolated in the main thread.

I set up two pipes for each thread (write to/read from) for message
passing.  The read-from-thread pipe is passed to gdk_input_add(). 
1-byte constants are used as a simple signalling protocol, which tells
either the thread or the main process to wake up and do work.

My best other alternative was to create a global gtk mutex, which would
have to be acquired by any thread before calling a glib, gdk, or gtk
function.  This didn't seem very clean at all compared to the previous
solution.

If you need to do some drawing, do it with Xlib drawing calls you know
are thread-safe, or with some other thread-safe drawing lib.  Then
signal the main process to grab the new image and blit it to the screen.

	Jeff



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