Re: GTK and threads
- From: jcupitt gmail com
- To: "Bernhard Jung" <bernhard bernhardjung de>
- Cc: gtk-list gnome org
- Subject: Re: GTK and threads
- Date: Tue, 29 Jul 2008 16:05:28 +0100
Hi,
2008/7/29 Bernhard Jung <bernhard bernhardjung de>:
> I'm currently having a problem in a application where I try to update a
> GtkListStore from a thread. When calling gtk_list_store_set I often get error
> messages that repeat
GTK doesn't work like this. You can call gtk from more than one
thread, but you need to be very careful with locking, and you will
find you have problems getting it working on win32.
In my opinion, the best way to do threading in gtk is to keep all
gtk_*() calls in a single GUI thread. Have a set of worker threads
which do the background stuff and have them pass packets of processed
data to the GUI to update the display on their behalf.
This is very easy to do, it turns out, with g_idle_add(). Unusally,
this function can be safely called from a background thread with no
need for any locks. Something like (untested):
// the stuff the bg thread calculates
typedef struct _Packet {
double data[100];
} Packet;
// run as a background thread ... generate a packet of data
// every second
void background_task (void *stuff)
{
for(;;) {
Packet *packet = g_new (Packet, 1);
int j;
for (j = 0; j < 100; j++)
packet->data[j] = (double) rand() / RAND_MAX;
sleep (1);
g_idle_add (new_packet, packet);
}
}
// this is run by the main GUI thread every time a new
// packet of data arrives
gboolean new_packet (Packet *packet)
{
int j;
for (j = 0; j < 100; j++)
update_gui (packet->data[j]);
g_free (packet);
// remove this idle function
// we only want to run once
return FALSE;
}
John
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]