Re: Fun with multithreaded gnome application



It may not be necessary to use threads for this situation.  Take a look at the
gtk_timeout_add() function.  You can use this to call a function periodically that
will run concurrently with the main loop.  If you pass the pointer to your progress
bar to this function, you can update it without having to use pipes/threads.

You'd call the function with something like this:

gtk_timeout_add(time, (GtkFunction)computation_function, (gpointer) progressbar);

and your timeout function would look something like this:

gint computation_function(gpointer data)
{
        while (!done) {
                [computations]
                gtk_progress_bar_update(GTK_PROGRESS_BAR(data), percent);
                while (gtk_events_pending())
                        gtk_main_iteration();
        }
        return FALSE;
}

You need to include the while loop so the GUI gets updated while you are in the
computation_function.  Also, returning FALSE means this function will not be called
again.  Otherwise it will be called every (time) milliseconds.


-Josh


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.

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?

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

Cheers,

Adam

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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