1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
>Is GTK+ thread safe? How do I write multi-threaded GTK+
6
applications? [GTK 2.x]</TITLE
9
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
12
HREF="book1.html"><LINK
14
TITLE="Development with GTK+: general questions"
15
HREF="c476.html"><LINK
17
TITLE="Development with GTK+: general questions"
18
HREF="c476.html"><LINK
20
TITLE="I'm doing some stuff with GTK+ in a separate thread, and
21
properly locking with gdk_threads_enter/gdk_threads_leave()
22
but the display doesn't update properly. [GTK 2.x]"
23
HREF="x492.html"></HEAD
34
SUMMARY="Header navigation table"
53
><<< Previous</A
59
>Development with GTK+: general questions</TD
67
>Next >>></A
80
>Is GTK+ thread safe? How do I write multi-threaded GTK+
87
>The GLib library can be used in a thread-safe mode by
88
calling g_thread_init() before making any other GLib
89
calls. In this mode GLib automatically locks all internal
90
data structures as needed. This does not mean that two
91
threads can simultaneously access, for example, a single hash
92
table, but they can access two different hash tables
93
simultaneously. If two different threads need to access the
94
same hash table, the application is responsible for locking
97
>In order to make GDK thread aware, you also need to
98
call gdk_threads_init() in conjunction with the above call.
99
There is a single global
100
lock that you must acquire with gdk_threads_enter() before
101
making any GDK calls, and release with gdk_threads_leave()
102
afterwards throughout your code.</P
104
>A minimal main program for a threaded GTK+ application
113
CLASS="PROGRAMLISTING"
115
main (int argc, char *argv[])
124
gtk_init(&argc, &argv);
126
window = create_window();
127
gtk_widget_show(window);
139
>Callbacks require a bit of attention. Callbacks from
140
GTK+ (signals) are made within the GTK+ lock. However
141
callbacks from GLib (timeouts, IO callbacks, and idle
142
functions) are made outside of the GTK+ lock. So, within a
143
signal handler you do not need to call gdk_threads_enter(),
144
but within the other types of callbacks, you do.</P
146
>Erik Mouw contributed the following code example to
147
illustrate how to use threads within GTK+ programs.</P
155
CLASS="PROGRAMLISTING"
156
>/*-------------------------------------------------------------------------
157
* Filename: gtk-thread.c
159
* Copyright: Copyright (C) 1999, Erik Mouw
160
* Author: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
161
* Description: GTK threads example.
162
* Created at: Sun Oct 17 21:27:09 1999
163
* Modified by: Owen Taylor <otaylor@gtk.org>
164
* Modified at: Wed May 28 10:43:00 2003
165
*-----------------------------------------------------------------------*/
169
* cc -o gtk-thread gtk-thread.c `pkg-config --cflags --libs gtk+-2.0 gthread-2.0`
171
* Thanks to Sebastian Wilhelmi for pointing out some bugs in earlier versions.
175
#include <unistd.h>
176
#include <gtk/gtk.h>
178
#define YES_IT_IS (1)
179
#define NO_IT_IS_NOT (0)
187
G_LOCK_DEFINE_STATIC (yes_or_no);
188
static volatile int yes_or_no = YES_IT_IS;
190
void destroy(GtkWidget *widget, gpointer data)
195
void *argument_thread(void *args)
197
yes_or_no_args *data = (yes_or_no_args *)args;
198
gboolean say_something;
203
sleep(g_random_int_range (1, 4));
205
/* lock the yes_or_no_variable */
208
/* do we have to say something? */
209
say_something = (yes_or_no != data->what);
213
/* set the variable */
214
yes_or_no = data->what;
217
/* Unlock the yes_or_no variable */
222
/* get GTK thread lock */
226
if(data->what == YES_IT_IS)
227
gtk_label_set_text(GTK_LABEL(data->label), "O yes, it is!");
229
gtk_label_set_text(GTK_LABEL(data->label), "O no, it isn't!");
231
/* Make sure all X commands are sent to the X server; not strictly
232
* necessary here, but always a good idea when you do anything
233
* from a thread other than the one where the main loop is running.
237
/* release GTK thread lock */
245
int main(int argc, char *argv[])
249
GError *error = NULL;
250
yes_or_no_args yes_args, no_args;
257
gtk_init(&argc, &argv);
259
/* create a window */
260
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
262
g_signal_connect(window, "destroy",
263
G_CALLBACK(destroy), NULL);
265
gtk_container_set_border_width(GTK_CONTAINER (window), 10);
268
label = gtk_label_new("And now for something completely different ...");
269
gtk_container_add(GTK_CONTAINER(window), label);
271
/* show everything */
272
gtk_widget_show(label);
273
gtk_widget_show (window);
275
/* create the threads */
276
yes_args.label = label;
277
yes_args.what = YES_IT_IS;
278
if (!g_thread_create(argument_thread, &yes_args, FALSE, &error))
280
g_printerr ("Failed to create YES thread: %s\n", error->message);
284
no_args.label = label;
285
no_args.what = NO_IT_IS_NOT;
286
if (!g_thread_create(argument_thread, &no_args, FALSE, &error))
288
g_printerr ("Failed to create NO thread: %s\n", error->message);
292
/* enter the GTK main loop */
308
SUMMARY="Footer navigation table"
321
><<< Previous</A
339
>Next >>></A
347
>Development with GTK+: general questions</TD
361
>I'm doing some stuff with GTK+ in a separate thread, and
362
properly locking with gdk_threads_enter/gdk_threads_leave()
363
but the display doesn't update properly. <I
b'\\ No newline at end of file'