Re: C++ + Gtkmm + Thread + new window



On Tue, 2007-02-27 at 19:57 +0100, Mateusz Szygenda wrote:
I use, gtkmm-2.4 and i'm writing a simple InstantMessager. I created a 
simple GUI using glademm and everything works fine, but i wanted to open 
conversation window when someone send message, so i created method which 
check for new messages and if there is some message it create new 
"conversation window". I run this method in new thread...and then the 
problem started. When method call "ConversationWindow" constructor, i 
receive many errors from GTK. I think method is okay(if i call this 
method without creating a new thread everything works fine, but app 
hangs up :) ).

In general, you can only call GTK+ from one thread in your application
(this applies to all its bindings, such as GTKmm, too).  There are a few
things you can do wrapped in gdk_threads_enter and gdk_threads_leave,
but in general, keep all your GTK+ calls in one thread.  If you need
additional worker threads for network connectivity or something, fine,
but they'll need to communicate with your GUI thread using GDispatcher
and/or some thread-safe asynchronous queue mechanism.

You basically have two options, for your situation:
     1. Use threads, offloading communication into worker threads, with
        all the GUI still in the main thread.
     2. Use asynchronous I/O (GLib can integrate this in to your main
        loop, calling functions when data is available or a pipe is
        ready to send) and keep everything in one thread.

Option 2 is probably easier to get right.  Much less can go wrong in
strange ways.  But whichever way you go, GTK+ can only work in one
thread.

- Michael

-- 
Michael Ekstrand
Research Assistant, Scalable Computing Laboratory
Goanna, compute cluster and InfiniBand network monitor tool:
        http://www.scl.ameslab.gov/Projects/Monitor/




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