Continuing the saga Non-Blocking GUI developement



Well, i spent today re-writing my client GUI program, and as per the suggestions I have received I did it using gthread. This scenario was very easy to write and allowed me to once more thread off the back end code and leave the GUI running in the main thread.

This whole program runs flawlessly, on my dual processor desktop. Unfortunately when I compile and run it on my single processor laptop the GUI becomes completely un-responsive. Basically all the backend client/server communications code runs perfectly, however I loose control of the program because the GUI effectively crashes on my single CPU laptop.

I have no idea how to fix this, I seemed to have made the fatal assumption that linux can time slice gthread code... Is this the problem?

Once again, thank you for any help you can provide.

Sincerely
Melvin

On 24 Jan 2007 22:43:15 +0100, Soeren Sandmann <sandmann daimi au dk> wrote:
"Melvin Newman" <zexelon gmail com> writes:

> Basically I am writing a program that is to interface with a server program
> over a network. As such this client program has effectively 2 parts, 1) the
> backend code that connects to the server and handles all the data
> communications, 2) the front end GUI that presents the code to the user.

As other people have said, the only sane way to use threads with GTK+
is to

   - only ever call gtk_* functions from the main thread

   - when a thread produces something that should be displayed,
     do

         g_idle_add (some_function,
                     pointer_to_data_that_the_thread_produced);

This will arrange for some_function() to be called in the main thread,
and be passed the pointer_to_data_that... pointer. The some_function()
can then safely call whatever gtk_* functions it wants.

The equivalent can also be done using GAsyncQueue, but g_idle_add() is
works fine and is simpler, and you can rely on a sequence of
g_idle_add()ed function to be executed in the same order they were
added.

To use non-blocking I/O with gtk+, you will need to get the server
file descriptor into the main poll() loop that GTK+ runs. The easiest
way to do this, in my opinion, is with this MIT licensed code:

    http://www.daimi.au.dk/~sandmann/watch/

that allows you to simply install callbacks to be called whenever
something interesting happens with the file descriptor.

GLib itself also has support for watching file descriptors; it's
called GIOChannel. I would not recommend using it though.



Soren



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