Re: Xlib-error in threaded GTK+



Tomas Berndtsson <tomas@nocrew.org> writes:

> Owen Taylor <otaylor@redhat.com> writes:
> 
> > > So, how do I avoid the error? Do I put another pair of
> > > gdk_threads_enter/gdk_threads_leave around the GTK+ calls in the
> > > second thread?
> > 
> > Well - the whole point of gdk_threads_enter() / gdk_threads_leave ()
> > is to allow multiple threads to coordinate access to GTK+. (They
> > are unnecessary if you are only making GTK+ calls from a single thread
> > thread.)
> > 
> > So, yes, making full use of these calls would be one way of approaching
> > the problem. (You'll want to read the threading section of the GTK+ 
> > FAQ and look at the example there.)
> > 
> > Another approach would be to figure out a way of communicating from
> > the second thread to the first thread and letting the first thread
> > do the work. You could use pipes, or you could take advantage 
> > of the fact that the GLib main loop is fully thread safe, do:
> > 
> >  g_idle_add (function_to_run_in_main_thread, data_for_function);
> > 
> > The first approach is preferable if the GUI work is heavily split
> > up between the threads, the second may be easier if you are mostly
> > doing GUI work in the main thread, but have one or two things you
> > want to trigger from the second thread.
> 
> Ok, thanks a lot!
> 
> I'll try one of these, or perhaps try both, and see what's best. They
> both seems fairly easy to put in. If I want my function to return a
> value, when using the g_idle_add() method, I suppose I have to put it
> all in a struct which is passed in the data_for_function, right? 

If you want it to return a value, yes, you would have to do something
like that, and then add some extra logic to wait for the idle function
to execute. (Using a semaphore perhaps)
 
> Are there more overhead in any of them? The small extra thing I have
> in the second thread, is called quite a lot of times, so if one of
> them is better for small, frequent routines, I guess that would be
> best to use.

Adding an idle has quite a bit more overhead than doing the locking
the normal way, there is a lot of code manipulating lists of main-loop
sources, etc.  gdk_thread_enter() / gdk_thread_leave() should be very
efficient, as long as the main thread doesn't want the GDK lock at the
same time.

Regards,
                                        Owen




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