Re: Non-Blocking GUI developement



Thank you very much for your help. I figured out what the problem is, I was making gtk calls from the second thread. This worked just fine for some reason on my dual proc machine, but causes the main gtk thread to lock up on my single cpu machine (it does not use 100% resources, it stays at about 14% cpu usage, but the gui locks up).

I am now trying to use g_idle_add(), however I cant seem to figure out how to pass data into the function that g_idle_add calls.

    g_idle_add(update_server_version,data.data);

above is the call I am making, data.data is defined as char * and no mater what i do, the string gets completely corrupted when it gets passed into the function.

In the interm i have implemented a hack, whereby I am using a global variable to pass data between the update_server_version() function and my thread. I really do not like this approach but nothing i try works. On a side note, I have the exact same problems when using the pthread_creat() function in other programs, so I am quite sure I am simply missing some key syntax issue.

Sincerely
Melvin Newman


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]