[gtkmm] threads



Hi,

I have a multithreaded app that I'm building out of separate components.  
I'd like to create a GTK viewer to render images generated by the one of 
the components.

I'm not using Glib or Gtk at all in the other components.  I'd like to
keep it this way if possible, because we have a bunch of our own libraries
that provide Glib-like functions, and not all users of these other
components will use the Gtk viewer.  Many of them will want to plug these
components into a different GUI toolkit, for example.

So I'm trying to encapsulate all the Glib and Gtk code in the GTK viewer 
object itself, without any of it spilling over to the other classes.

I'm new to gtkmm, glib and GTK, so I've got a couple of questions.  I've
checked the FAQs and example programs, but those all appear to assume that 
you're using Glib, sigc++, etc. throughout your program.  My case is a 
little different.

There's one component that generates new images.  Let's call it the Scene
object.  The Scene object needs to tell the "Viewer"  object when a new
image is available via a Viewer::notify method.  The Viewer will then make
a copy of it and tell the Scene that it's done, so that the image can be
destroyed/recycled/whatever.  (This is your basic model-view interaction.)

After reading the gtkmm FAQ, it sounds like I want to use Glib::Dispatcher
to do this.  The Viewer will create the Glib::Dispatcher object and
connect it to the correct callback in the Viewer thread.  When the Scene
thread calls the GtkViewer::notify method, it will invoke the Dispatcher.  
The GtkViewer knows how to get the image from the Scene.

So I've got a few questions:

1. Currently, the Scene thread is the only thread that'll be invoking the 
Dispatcher, i.e., it's the only sender thread.  There's only one GtkViewer 
object and it is single-threaded.  Do I need to call Glib::thread_init in 
the GtkViewer?

2. Is it safe for the Scene thread to invoke the Glib::Dispatcher via the 
GtkViewer::notify method, even though that Scene thread doesn't make any 
other Glib calls?

3. Assuming that at some point in the future I create multiple Scene 
threads, each of which want to send "copy" messages to the single-threaded 
GtkViewer, do I need to put a lock around the Dispatcher in the 
GtkViewer::notify method?  From the Glib::Dispatcher documentation page, 
it sounds like not, but it's not clear to me whether that assumes that 
each sender thread has its own instance of a Dispatcher.  In the scenario 
I'm describing, there is only one Dispatcher object, and it belongs to the 
single GtkViewer object.

Hope that wasn't too confusing ;)

thanks much!
-dwh-






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