Re: Non-Blocking GUI developement



"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]