summaryrefslogtreecommitdiff
path: root/docs/reference/gdk/tmpl/threads.sgml
blob: 0400280ca20cb8b18cd56d18a2c648350b4f5501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!-- ##### SECTION Title ##### -->
Threads

<!-- ##### SECTION Short_Description ##### -->
Functions for using GDK in multi-threaded programs

<!-- ##### SECTION Long_Description ##### -->
<para>
For thread safety, GDK relies on the thread primitives in GLib, 
and on the thread-safe GLib main loop.
</para>
<para>
GLib is completely thread safe (all global data is automatically 
locked), but individual data structure instances are not automatically 
locked for performance reasons. So e.g. you must coordinate 
accesses to the same #GHashTable from multiple threads.
</para>
<para>
GTK+ is "thread aware" but not thread safe &mdash; it provides a
global lock controlled by gdk_threads_enter()/gdk_threads_leave()
which protects all use of GTK+. That is, only one thread can use GTK+ 
at any given time.
</para>
<para>
Unfortunately the above holds with the X11 backend only. With the
Win32 backend, GDK calls should not be attempted from multiple threads
at all.
</para>
<para>
You must call g_thread_init() and gdk_threads_init() before executing
any other GTK+ or GDK functions in a threaded GTK+ program.
</para>
<para>
Idles, timeouts, and input functions are executed outside
of the main GTK+ lock. So, if you need to call GTK+
inside of such a callback, you must surround the callback
with a gdk_threads_enter()/gdk_threads_leave() pair.
(However, signals are still executed within the main
GTK+ lock.)
</para>
<para>
In particular, this means, if you are writing widgets that might 
be used in threaded programs, you <emphasis>must</emphasis> surround 
timeouts and idle functions in this matter.
</para>
<para>
As always, you must also surround any calls to GTK+ not made within 
a signal handler with a gdk_threads_enter()/gdk_threads_leave() pair.
</para>

<para>
Before calling gdk_threads_leave() from a thread other
than your main thread, you probably want to call gdk_flush()
to send all pending commands to the windowing system.
(The reason you don't need to do this from the main thread
is that GDK always automatically flushes pending commands
when it runs out of incoming events to process and has
to sleep while waiting for more events.)
</para>

<para>A minimal main program for a threaded GTK+ application
looks like:</para>

<informalexample>
<programlisting role="C">
int
main (int argc, char *argv[])
{
  GtkWidget *window;

  g_thread_init (NULL);
  gdk_threads_init (<!-- -->);
  gdk_threads_enter (<!-- -->);

  gtk_init (&amp;argc, &amp;argv);

  window = create_window (<!-- -->);
  gtk_widget_show (window);

  gtk_main (<!-- -->);
  gdk_threads_leave (<!-- -->);

  return 0;
}
</programlisting>
</informalexample>

<para>
Callbacks require a bit of attention. Callbacks from GTK+ signals
are made within the GTK+ lock. However callbacks from GLib (timeouts,
IO callbacks, and idle functions) are made outside of the GTK+
lock. So, within a signal handler you do not need to call
gdk_threads_enter(), but within the other types of callbacks, you
do.
</para>

<para>Erik Mouw contributed the following code example to
illustrate how to use threads within GTK+ programs.
</para>

<informalexample>
<programlisting role="C">
/*-------------------------------------------------------------------------
 * Filename:      gtk-thread.c
 * Version:       0.99.1
 * Copyright:     Copyright (C) 1999, Erik Mouw
 * Author:        Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
 * Description:   GTK threads example. 
 * Created at:    Sun Oct 17 21:27:09 1999
 * Modified by:   Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
 * Modified at:   Sun Oct 24 17:21:41 1999
 *-----------------------------------------------------------------------*/
/*
 * Compile with:
 *
 * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
 *
 * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
 * bugs.
 *
 */

&num;include &lt;stdio.h&gt;
&num;include &lt;stdlib.h&gt;
&num;include &lt;unistd.h&gt;
&num;include &lt;time.h&gt;
&num;include &lt;gtk/gtk.h&gt;
&num;include &lt;glib.h&gt;
&num;include &lt;pthread.h&gt;

&num;define YES_IT_IS    (1)
&num;define NO_IT_IS_NOT (0)

typedef struct 
{
  GtkWidget *label;
  int what;
} yes_or_no_args;

G_LOCK_DEFINE_STATIC (yes_or_no);
static volatile int yes_or_no = YES_IT_IS;

void destroy (GtkWidget *widget, gpointer data)
{
  gtk_main_quit (<!-- -->);
}

void *argument_thread (void *args)
{
  yes_or_no_args *data = (yes_or_no_args *)args;
  gboolean say_something;

  for (;;)
    {
      /* sleep a while */
      sleep(rand(<!-- -->) / (RAND_MAX / 3) + 1);

      /* lock the yes_or_no_variable */
      G_LOCK(yes_or_no);

      /* do we have to say something? */
      say_something = (yes_or_no != data->what);

      if(say_something)
	{
	  /* set the variable */
	  yes_or_no = data->what;
	}

      /* Unlock the yes_or_no variable */
      G_UNLOCK (yes_or_no);

      if (say_something)
	{
	  /* get GTK thread lock */
	  gdk_threads_enter (<!-- -->);

	  /* set label text */
	  if(data->what == YES_IT_IS)
	    gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
	  else
	    gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");

	  /* release GTK thread lock */
	  gdk_threads_leave (<!-- -->);
	}
    }

  return NULL;
}

int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *label;
  yes_or_no_args yes_args, no_args;
  pthread_t no_tid, yes_tid;

  /* init threads */
  g_thread_init (NULL);
  gdk_threads_init (<!-- -->);
  gdk_threads_enter (<!-- -->);

  /* init gtk */
  gtk_init(&amp;argc, &amp;argv);

  /* init random number generator */
  srand ((unsigned int) time (NULL));

  /* create a window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC (destroy), NULL);

  gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  /* create a label */
  label = gtk_label_new ("And now for something completely different ...");
  gtk_container_add (GTK_CONTAINER (window), label);
  
  /* show everything */
  gtk_widget_show (label);
  gtk_widget_show (window);

  /* create the threads */
  yes_args.label = label;
  yes_args.what = YES_IT_IS;
  pthread_create (&amp;yes_tid, NULL, argument_thread, &amp;yes_args);

  no_args.label = label;
  no_args.what = NO_IT_IS_NOT;
  pthread_create (&amp;no_tid, NULL, argument_thread, &amp;no_args);

  /* enter the GTK main loop */
  gtk_main (<!-- -->);
  gdk_threads_leave (<!-- -->);

  return 0;
}
</programlisting>
</informalexample>

<!-- ##### SECTION See_Also ##### -->
<para>

</para>

<!-- ##### SECTION Stability_Level ##### -->


<!-- ##### MACRO GDK_THREADS_ENTER ##### -->
<para>
This macro marks the beginning of a critical section in which GDK and GTK+
functions can be called.  Only one thread at a time can be in such a
critial section. The macro expands to a no-op if #G_THREADS_ENABLED
has not been defined. Typically gdk_threads_enter() should be used 
instead of this macro.
</para>



<!-- ##### MACRO GDK_THREADS_LEAVE ##### -->
<para>
This macro marks the end of a critical section 
begun with #GDK_THREADS_ENTER.
</para>



<!-- ##### FUNCTION gdk_threads_init ##### -->
<para>

</para>



<!-- ##### FUNCTION gdk_threads_enter ##### -->
<para>
This macro marks the beginning of a critical section
in which GDK and GTK+ functions can be called.
Only one thread at a time can be in such a critial 
section.
</para>



<!-- ##### FUNCTION gdk_threads_leave ##### -->
<para>
Leaves a critical region begun with gdk_threads_enter(). 
</para>



<!-- ##### VARIABLE gdk_threads_mutex ##### -->
<para>
The #GMutex used to implement the critical region for
gdk_threads_enter()/gdk_threads_leave(). 
</para>

@Deprecated: 2.4: This variable should not be used directly 
  &mdash; consider it private.


<!-- ##### FUNCTION gdk_threads_set_lock_functions ##### -->
<para>

</para>

@enter_fn: 
@leave_fn: