Re: Fun with multithreaded gnome application



Adam Cherrett wrote:

I've just started on my first GTK+/gnome application, and am very pleased
with the ease with which I'm making progress. However, I've just come up
against an interesting problem.

Because the application involves lengthy computations, it is essential that
I multithread it. I've discovered that making any gtk calls from anything
other than the main thread (which is the one running gtk_main()) is doomed
to failure. However, I want progress information to be displayed for one of
my computation threads.

Threads _can_ be used! See section 5.2 of the faq at www.gtk.org.

From reading various posts on similar situations, this is the way I intend
to solve the problem:

1. Open a pipe to communicate between the two threads
2. Send a 'message' with occasional increments in the progress gauge
   (actually a pointer to some mutex-protected shared memory to save
    time)
3. Wake up the main thread whenever a message arrives and act on it.

Firstly, does this sound like a sane solution?

This sounds reasonable and has the advantage that there is no GUI code mixed in
with the calculation routine.

I obviously don't know what the code is doing but if you do use a pipe couldn't
you just write the new progress value as an integer to the pipe? That would
save having to mess around with shared mem and mutexes. Just a thought - might
not fit in with your code though.

Secondly, how do I arrange for the gtk main loop to watch the pipe and run a
callback function whenever something arrives?

Use either the g_io_channel_xxx functions (in glib.h) or gdk_input_add() (in
gdk.h).

Note that for the io channel stuff there is a convenience function,
g_io_channel_unix_new(), which creates a GIOChannel for a UNIX file descriptor.

Having said that I have only ever used gdk_input_add() which seems to work fine
and is very easy to use.

Hope this helps.

Cheers, Matt.

--
Matt Goodall, Software Engineer  |  Isotek Electronics Ltd
mailto:mgg isotek co uk          |  Claro House, Servia Road
http://www.isotek.co.uk          |  Leeds, LS7 1NL
Tel: +44 113 234320              |  England







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