Initial revision
[platform/upstream/glib.git] / docs / reference / glib / tmpl / threads.sgml
1 <!-- ##### SECTION Title ##### -->
2
3 Threads
4
5 <!-- ##### SECTION Short_Description ##### -->
6
7 thread abstraction; including mutexes, conditions and thread private data.
8
9 <!-- ##### SECTION Long_Description ##### -->
10
11 <para>
12 Threads act almost like processes, but unlike processes all threads of
13 one process share the same memory. This is good, as it provides easy
14 communication between the involved threads via this shared memory, and
15 it is bad, because strange things (so called Heisenbugs) might happen,
16 when the program is not carefully designed. Especially bad is, that due
17 to the concurrent nature of threads no assumptions on the order of
18 execution of different threads can be done unless explictly forced by
19 the programmer through synchronization primitives.
20 </para>
21
22 <para>
23 The aim of the thread related functions in GLib is to provide a
24 portable means for writing multithread safe software. There are
25 primitives for mutexes to protect the access to portions of memory
26 (#GMutex, #GStaticMutex, #G_LOCK_DEFINE and friends), there are
27 primitives for condition variables to allow synchronization of threads
28 (#GCond) and finally there are primitives for thread-private data,
29 that every thread has a private instance of (#GPrivate,
30 #GStaticPrivate).
31 </para>
32
33 <para>
34 Currently there is only as much thread support included in GLib as is
35 necessary to make GLib itself multithread safe. Future versions of
36 GLib might contain functions to actually create threads and the
37 like. For now the most portable way to create threads is to require
38 the macro #G_THREADS_IMPL_POSIX to be defined and use POSIX threads
39 then. This will work on almost all platforms (except most notably
40 Solaris).
41 </para>
42
43 <!-- ##### SECTION See_Also ##### -->
44 <para>
45
46 </para>
47
48 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
49
50 <para>
51 This macro is defined, if GLib was compiled with thread support. This
52 does not necessarily mean, that there is a thread implementation
53 available, but the infrastructure is in place and once you provide a
54 thread implementation to g_thread_init(), GLib will be multithread
55 safe. It isn't and can't be, if #G_THREADS_ENABLED is not defined.
56 </para>
57
58
59
60 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
61
62 <para>
63 This macro is defined, if POSIX style threads are used.
64 </para>
65
66
67
68 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
69
70 <para>
71 This macro is defined, if the SOLARIS thread system is used.
72 </para>
73
74
75
76 <!-- ##### MACRO G_THREADS_IMPL_NSPR ##### -->
77
78 <para>
79 This macro is defined, if the NSPR thread implementation is used.
80 NSPR is the cross platform library of mozilla.
81 </para>
82
83
84
85 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
86
87 <para>
88 This macro is defined, if no thread implementation is used. You can
89 however provide one to g_thread_init() to make GLib multithread safe.
90 </para>
91
92
93
94 <!-- ##### STRUCT GThreadFunctions ##### -->
95
96 <para>
97 This function table is used by g_thread_init() to initialize the
98 thread system. The functions in that table are directly used by their
99 g_* prepended counterparts, that are described here, e.g. if you call
100 g_mutex_new() then mutex_new() from the table provided to
101 g_thread_init() will be called.
102 </para>
103
104 <note>
105 <para>
106 This struct should only be used, if you know, what you are doing.
107 </para>
108 </note>
109
110 @mutex_new: 
111 @mutex_lock: 
112 @mutex_trylock: 
113 @mutex_unlock: 
114 @mutex_free: 
115 @cond_new: 
116 @cond_signal: 
117 @cond_broadcast: 
118 @cond_wait: 
119 @cond_timed_wait: 
120 @cond_free: 
121 @private_new: 
122 @private_get: 
123 @private_set: 
124
125 <!-- ##### FUNCTION g_thread_init ##### -->
126
127 <para>
128 Before you use a thread related function in GLib, you should
129 initialize the thread system. This is done by calling
130 g_thread_init(). Most of the time you will only have to call
131 g_thread_init(NULL). 
132 </para>
133
134 <note>
135 <para>
136 You should only call g_thread_init() with a non-NULL parameter, if you
137 really know, what you are doing.
138 </para>
139 </note>
140
141 <note>
142 <para>
143 g_thread_init() must not be called directly or indirectly as a
144 callback from GLib.
145 </para>
146 </note>
147
148 <para>
149 g_thread_init() might only be called once. On the second call
150 it will abort with an error. If you want to make sure, that the thread
151 system is initialized, you can do that too:
152 </para>
153
154 <para>
155 <informalexample>
156 <programlisting>
157 if (!g_thread_supported ()) g_thread_init (NULL);
158 </programlisting>
159 </informalexample>
160 </para>
161
162 <para>
163 After that line either the thread system is initialized or the program
164 will abort, if no thread system is available in GLib, i.e. either
165 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
166 </para>
167
168 <para>
169 If no thread system is available and @vtable is NULL or if not all
170 elements of @vtable are non-NULL, then g_thread_init() will abort.
171 </para>
172
173 <note>
174 <para>
175 To use g_thread_init() in your program, you have to link with the
176 libraries, that the command "glib-config --libs gthread" outputs. This
177 is not the case for all the other thread related functions of
178 GLib. Those can be used without having to link with the thread
179 libraries.
180 </para>
181 </note>
182
183 @vtable: a function table of type #GThreadFunctions, that provides the
184 entry points to the thread system to be used.
185
186
187 <!-- ##### MACRO g_thread_supported ##### -->
188 <para>
189 This function returns, whether the thread system is initialized or
190 not.
191 </para>
192
193 <note>
194 <para>
195 This function is actually a macro. Apart from taking the address of it
196 you can however use it as if it was a function.
197 </para>
198 </note>
199
200 @Returns: TRUE, if the thread system is initialized.
201
202
203 <!-- ##### STRUCT GMutex ##### -->
204
205 <para>
206 The #GMutex struct is an opaque data structure to represent a mutex
207 (mutual exclusion). It can be used to protect data against shared
208 access. Take for example the following function:
209
210 <para>
211 <example>
212 <title>A function which will not work in a threaded environment</title>
213 <programlisting>
214   int give_me_next_number ()
215   {
216     static int current_number = 0;
217
218     /* now do a very complicated calculation to calculate the new number,
219        this might for example be a random number generator */
220     current_number = calc_next_number (current_number); 
221     return current_number;
222   }
223 </programlisting>
224 </example>
225 </para>
226
227 <para>
228 It is easy to see, that this won't work in a multithreaded
229 application. There current_number must be protected against shared
230 access. A first naive implementation would be:
231 </para>
232
233 <para>
234 <example>
235 <title>The wrong way to write a thread-safe function</title>
236 <programlisting>
237   int give_me_next_number ()
238   {
239     static int current_number = 0;
240     int ret_val;
241     static GMutex * mutex = NULL;
242
243     if (!mutex)
244       mutex = g_mutex_new ();
245     g_mutex_lock (mutex);
246     ret_val = current_number = calc_next_number (current_number); 
247     g_mutex_unlock (mutex);
248     return ret_val;
249   }
250 </programlisting>
251 </example>
252 </para>
253
254 <para>
255 This looks like it would work, but there is a race condition while
256 constructing the mutex and this code can't work reliable. So please do
257 not use such constructs in your own programs. One working solution is:
258 </para>
259
260 <para>
261 <example>
262 <title>A correct thread-safe function</title>
263 <programlisting>
264   static GMutex *give_me_next_number_mutex = NULL;
265
266   /* this function must be called before any call to give_me_next_number ()
267      it must be called exactly once. */
268   void init_give_me_next_number () 
269   {
270     g_assert (give_me_next_number_mutex == NULL);
271     give_me_next_number_mutex = g_mutex_new ();
272   }
273
274   int give_me_next_number ()
275   {
276     static int current_number = 0;
277     int ret_val;
278
279     g_mutex_lock (give_me_next_number_mutex);
280     ret_val = current_number = calc_next_number (current_number); 
281     g_mutex_unlock (give_me_next_number_mutex);
282     return ret_val;
283   }
284 </programlisting>
285 </example>
286 </para>
287
288 <para>
289 #GStaticMutex provides a simpler and safer way of doing this.
290 </para>
291
292 <para>
293 A #GMutex should only be accessed via the following functions.
294 </para>
295
296 <note>
297 <para>
298 All of the g_mutex_* functions are actually macros. Apart from taking
299 the addresses of them, you can however use them as if they were functions.
300 </para>
301 </note>
302
303
304 <!-- ##### MACRO g_mutex_new ##### -->
305
306 <para>
307 Creates a new #GMutex. 
308 </para>
309
310 <note>
311 <para>
312 This function will abort, if g_thread_init() has not been called yet.
313 </para>
314 </note>
315
316 @Returns: a new #GMutex.
317
318
319 <!-- ##### MACRO g_mutex_lock ##### -->
320
321 <para>
322 Locks the #GMutex. If the #GMutex is already locked by another thread,
323 the current thread will block until the #GMutex is unlocked by the
324 other thread.
325 </para>
326
327 <para>
328 This function can also be used, if g_thread_init() has not yet been
329 called and will do nothing then.
330 </para>
331
332 <note>
333 <para>
334 #GMutex is not guaranteed to be recursive, i.e. a thread might block,
335 if it already has locked the #GMutex. It will deadlock then, of
336 course.
337 </para>
338 </note>
339
340 @mutex: a #GMutex.
341
342
343 <!-- ##### MACRO g_mutex_trylock ##### -->
344
345 <para>
346 Tries to lock the #GMutex. If the #GMutex is already locked by another
347 thread, it immediately returns FALSE. Otherwise it locks the #GMutex
348 and returns TRUE.
349 </para>
350
351 <para>
352 This function can also be used, if g_thread_init() has not yet been
353 called and will immediately return TRUE then.
354 </para>
355
356 @mutex: a #GMutex.
357 @Returns: TRUE, if the #GMutex could be locked.
358
359
360 <!-- ##### MACRO g_mutex_unlock ##### -->
361
362 <para>
363 Unlocks the #GMutex. If another thread is blocked in a g_mutex_lock()
364 call, it will be woken and can lock the #GMutex itself. This function
365 can also be used, if g_thread_init() has not yet been called and will
366 do nothing then.
367 </para>
368
369 @mutex: a #GMutex.
370
371
372 <!-- ##### MACRO g_mutex_free ##### -->
373
374 <para>
375 Destroys the #GMutex.
376 </para>
377
378 @mutex: a #GMutex.
379
380
381 <!-- ##### STRUCT GStaticMutex ##### -->
382
383 <para>
384 A #GStaticMutex works like a #GMutex, but it has one significant
385 advantage. It doesn't need to be created at run-time like a #GMutex,
386 but can be defined at compile-time. Here is a shorter, easier and
387 safer version of our give_me_next_number() example:
388 </para>
389
390 <para>
391 <example>
392 <title>Using GStaticMutex to simplify thread-safe programming</title>
393 <programlisting>
394   int give_me_next_number ()
395   {
396     static int current_number = 0;
397     int ret_val;
398     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
399
400     g_static_mutex_lock (&amp;mutex);
401     ret_val = current_number = calc_next_number (current_number); 
402     g_static_mutex_unlock (&amp;mutex);
403     return ret_val;
404   }
405 </programlisting>
406 </example>
407 </para>
408
409 <para>
410 Even though #GStaticMutex is not opaque, it should only be used with
411 the following functions, as it is defined differently on different
412 platforms.
413 </para>
414
415 <para>All of the g_static_mutex_* functions can also be used, if
416 g_thread_init() has not yet.
417 </para>
418
419 <note>
420 <para>
421 All of the g_static_mutex_* functions are actually macros. Apart from
422 taking the addresses of them, you can however use them as if they were
423 functions.
424 </para>
425 </note>
426
427 @runtime_mutex: 
428
429 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
430
431 <para>
432 Every #GStaticMutex must be initialized with this macro, before it can
433 be used.
434 </para>
435
436 <para>
437 <example>
438 <title>Initializing a GStaticMutext</title>
439 <programlisting>
440 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
441 </programlisting>
442 </example>
443 </para>
444
445
446
447 <!-- ##### MACRO g_static_mutex_lock ##### -->
448 <para>
449 works like g_mutex_lock(), but for a #GStaticMutex.
450 </para>
451
452 @mutex: a #GStaticMutex.
453
454
455 <!-- ##### MACRO g_static_mutex_trylock ##### -->
456
457 <para>
458 works like g_mutex_trylock(), but for a #GStaticMutex.
459 </para>
460
461 @mutex: a #GStaticMutex.
462 @Returns: TRUE, if the #GStaticMutex could be locked.
463
464
465 <!-- ##### MACRO g_static_mutex_unlock ##### -->
466
467 <para>
468 works like g_mutex_unlock(), but for a #GStaticMutex.
469 </para>
470
471 @mutex: a #GStaticMutex.
472
473
474 <!-- ##### MACRO g_static_mutex_get_mutex ##### -->
475
476 <para>
477 For some operations (like g_cond_wait()) you must have a #GMutex
478 instead of a #GStaticMutex. This function will return the
479 corresponding #GMutex for every #GStaticMutex.
480 </para>
481
482 @mutex: a #GStaticMutex.
483 @Returns: the corresponding #GMutex.
484
485
486 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
487
488 <para>
489 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
490 with the advantage that they will expand to nothing in programs
491 compiled against a thread-disabled GLib, saving code and memory
492 there. #G_LOCK_DEFINE defines a lock. It can occur, where variable
493 definitions may occur in programs, i.e. in the first block of a
494 function or outside of functions. The @name parameter will be mangled
495 to get the name of the #GStaticMutex. This means, that you can use
496 names of existing variables as the parameter, e.g. the name of the
497 variable you intent to protect with the lock. Look at our
498 give_me_next_number() example using the G_LOCK_* macros:
499 </para>
500
501 <para>
502 <example>
503 <title>Using the G_LOCK_* convenience macros</title>
504 <programlisting>
505 int give_me_next_number ()
506   {
507     static int current_number = 0;
508     int ret_val;
509     G_LOCK_DEFINE_STATIC (current_number);
510
511     G_LOCK (current_number);
512     ret_val = current_number = calc_next_number (current_number); 
513     G_UNLOCK (current_number);
514     return ret_val;
515   }
516 </programlisting>
517 </example>
518 </para>
519
520 @name: the name of the lock.
521
522
523 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
524
525 <para>
526 This works like #G_LOCK_DEFINE, but it creates a static object.
527 </para>
528
529 @name: the name of the lock.
530
531
532 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
533
534 <para>
535 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
536 </para>
537
538 @name: the name of the lock.
539
540
541 <!-- ##### MACRO G_LOCK ##### -->
542
543 <para>
544 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
545 </para>
546
547 @name: the name of the lock.
548
549
550 <!-- ##### MACRO G_TRYLOCK ##### -->
551
552 <para>
553 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
554 </para>
555
556 @name: the name of the lock.
557 @Returns: TRUE, if the lock could be locked.
558
559
560 <!-- ##### MACRO G_UNLOCK ##### -->
561
562 <para>
563 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
564 </para>
565
566 @name: the name of the lock.
567
568
569 <!-- ##### STRUCT GCond ##### -->
570
571 <para>
572 The #GCond struct is an opaque data structure to represent a
573 condition. A #GCond is an object, that threads can block on, if they
574 find a certain condition to be false. If other threads change the
575 state of this condition they can signal the #GCond, such that the
576 waiting thread is woken up. 
577 </para>
578
579 <para>
580 <example>
581 <title>Using GCond to block a thread until a condition is satisfied</title>
582 <programlisting>
583 GCond* data_cond = NULL;   /* Must be initialized somewhere */
584 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
585 gpointer current_data = NULL;
586
587 void push_data (gpointer data)
588 {
589   g_mutex_lock (data_mutex);
590   current_data = data;
591   g_cond_signal (data_cond);
592   g_mutex_unlock (data_mutex);
593 }
594
595 gpointer pop_data ()
596 {
597   gpointer data;
598
599   g_mutex_lock (data_mutex);
600   while (!current_data)
601       g_cond_wait (data_cond, data_mutex);
602   data = current_data;
603   current_data = NULL;
604   g_mutex_unlock (data_mutex);
605   return data;
606 }
607 </programlisting>
608 </example>
609 </para>
610
611 <para>
612 Whenever a thread calls pop_data() now, it will wait until
613 current_data is non-NULL, i.e. until some other thread has called
614 push_data().
615 </para>
616
617 <note>
618 <para>
619 It is important to use the g_cond_wait() and g_cond_timed_wait()
620 functions only inside a loop, which checks for the condition to be
621 true as it is not guaranteed that the waiting thread will find it
622 fulfilled, even if the signaling thread left the condition
623 in that state. This is because another thread can have altered the
624 condition, before the waiting thread got the chance to be woken up,
625 even if the condition itself is protected by a #GMutex, like above.
626 </para>
627 </note>
628
629 <para>
630 A #GCond should only be accessed via the following functions.
631 </para>
632
633 <note>
634 <para>
635 All of the g_cond_* functions are actually macros. Apart from taking
636 the addresses of them, you can however use them as if they were functions.
637 </para>
638 </note>
639
640
641 <!-- ##### MACRO g_cond_new ##### -->
642
643 <para>
644 Creates a new #GCond. This function will abort, if g_thread_init()
645 has not been called yet.
646 </para>
647
648 @Returns: a new #GCond.
649
650
651 <!-- ##### MACRO g_cond_signal ##### -->
652 <para>
653 If threads are waiting for @cond, exactly one of them is woken up. It
654 is good practice to hold the same lock as the waiting thread, while
655 calling this function, though not required.
656 </para>
657
658 <para>
659 This function can also be used, if g_thread_init() has
660 not yet been called and will do nothing then.
661 </para>
662
663 @cond: a #GCond.
664
665
666 <!-- ##### MACRO g_cond_broadcast ##### -->
667
668 <para>
669 If threads are waiting for @cond, all of them are woken up. It is good
670 practice to lock the same mutex as the waiting threads, while calling
671 this function, though not required.
672 </para>
673
674 <para>
675 This function can also be used, if g_thread_init() has
676 not yet been called and will do nothing then.
677 </para>
678
679 @cond: a #GCond.
680
681
682 <!-- ##### MACRO g_cond_wait ##### -->
683
684 <para>
685 Waits until this thread is woken up on the #GCond. The #GMutex is
686 unlocked before falling asleep and locked again before resuming.
687 </para>
688
689 <para>
690 This function can also be used, if g_thread_init() has not yet been
691 called and will immediately return then.
692 </para>
693
694 @cond: a #GCond.
695 @mutex: the #GMutex, that is currently locked.
696
697
698 <!-- ##### MACRO g_cond_timed_wait ##### -->
699
700 <para>
701 Waits until this thread is woken up on the #GCond, but not longer than
702 until the time, that is specified by @abs_time. The #GMutex is
703 unlocked before falling asleep and locked again before resuming.
704 </para>
705
706 <para>
707 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
708 </para>
709
710 <para>
711 This function can also be used, if g_thread_init() has not yet been
712 called and will immediately return TRUE then.
713 </para>
714
715 @cond: a #GCond.
716 @mutex: the #GMutex, that is currently locked.
717 @abs_time: a #GTimeVal, determining the final time.
718 @Returns: TRUE, if the thread is woken up in time.
719
720
721 <!-- ##### MACRO g_cond_free ##### -->
722
723 <para>
724 Destroys the #GCond.
725 </para>
726
727 @cond: a #GCond.
728
729
730 <!-- ##### STRUCT GPrivate ##### -->
731 <para>
732 The #GPrivate struct is an opaque data structure to represent a thread
733 private data key. Threads can thereby obtain and set a pointer, which
734 is private to the current thread. Take our give_me_next_number()
735 example from above.  Now we don't want current_number to be shared
736 between the threads, but to be private to each thread. This can be
737 done as follows:
738
739 <para>
740 <example>
741 <title>Using GPrivate for per-thread data</title>
742 <programlisting>
743   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
744                                        /* with g_private_new (g_free); */
745
746   int give_me_next_number ()
747   {
748     int *current_number = g_private_get (current_number_key);
749
750     if (!current_number)
751     {
752       current_number = g_new (int,1);
753       *current_number = 0;
754       g_private_set (current_number_key, current_number);
755     }
756     *current_number = calc_next_number (*current_number); 
757     return *current_number;
758   }
759 </programlisting>
760 </example>
761 </para>
762
763 <para>
764 Here the pointer belonging to the key current_number_key is read. If
765 it is NULL, it has not been set yet. Then get memory for an integer
766 value, assign this memory to the pointer and write the pointer
767 back. Now we have an integer value, that is private to the current
768 thread.
769 </para>
770
771 <para>
772 The #GPrivate struct should only be accessed via the following functions.
773 </para>
774
775 <note>
776 <para>
777 All of the g_private_* functions are actually macros. Apart from taking
778 the addresses of them, you can however use them as if they were functions.
779 </para>
780 </note>
781
782
783 <!-- ##### MACRO g_private_new ##### -->
784
785 <para>
786 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
787 to a destructor function. Whenever a thread ends and the corresponding
788 pointer keyed to this instance of #GPrivate is non-NULL, the
789 destructor is called with this pointer as the argument.
790 </para>
791
792 <note>
793 <para>
794 The @destructor is working quite differently from @notify in
795 g_static_private_set().
796 </para>
797 </note>
798
799 <note>
800 <para>
801 A #GPrivate can not be destroyed. Reuse it instead, if you can to
802 avoid shortage.
803 </para>
804 </note>
805
806 <note>
807 <para>
808 This function will abort, if g_thread_init() has not been called yet.
809 </para>
810 </note>
811
812 @destructor: a function to handle the data keyed to #GPrivate, when a
813 thread ends.
814
815
816 <!-- ##### MACRO g_private_get ##### -->
817
818 <para>
819 Returns the pointer keyed to @private_key for the current thread. This
820 pointer is NULL, when g_private_set() hasn't been called for the
821 current @private_key and thread yet.
822 </para>
823
824 <para>
825 This function can also be used, if g_thread_init() has not yet been
826 called and will return the value of @private_key casted to #gpointer then.
827 </para>
828
829 @private_key: a #GPrivate.
830 @Returns: the corresponding pointer.
831
832
833 <!-- ##### MACRO g_private_set ##### -->
834
835 <para>
836 Sets the pointer keyed to @private_key for the current thread.
837 </para>
838
839 <para>
840 This function can also be used, if g_thread_init() has not yet been
841 called and will set @private_key to @data casted to #GPrivate* then.
842 </para>
843
844 @private_key: a #GPrivate.
845 @value: 
846 <!-- # Unused Parameters # -->
847 @data: the new pointer.
848
849
850 <!-- ##### STRUCT GStaticPrivate ##### -->
851
852 <para>
853 A #GStaticPrivate works almost like a #GPrivate, but it has one
854 significant advantage. It doesn't need to be created at run-time like
855 a #GPrivate, but can be defined at compile-time. This is similar to
856 the difference between #GMutex and #GStaticMutex. Now look at our
857 give_me_next_number() example with #GStaticPrivate:
858 </para>
859
860 <para>
861 <example>
862 <title>Using GStaticPrivate for per-thread data</title>
863 <programlisting>
864   int give_me_next_number ()
865   {
866     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
867     int *current_number = g_static_private_get (&amp;current_number_key);
868
869     if (!current_number)
870     {
871       current_number = g_new (int,1);
872       *current_number = 0;
873       g_static_private_set (&amp;current_number_key, current_number, g_free);
874     }
875     *current_number = calc_next_number (*current_number); 
876     return *current_number;
877   }
878 </programlisting>
879 </example>
880 </para>
881
882 @index: 
883
884 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
885 <para>
886 Every #GStaticPrivate must be initialized with this macro, before it can
887 be used.
888 </para>
889
890 <para>
891 <informalexample>
892 <programlisting>
893 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
894 </programlisting>
895 </informalexample>
896 </para>
897
898
899
900 <!-- ##### FUNCTION g_static_private_get ##### -->
901 <para>
902 Works like g_private_get() only for a #GStaticPrivate.
903 </para>
904
905 <para>
906 This function also works, if g_thread_init() has not yet been called.
907 </para>
908
909 @private_key: a #GStaticPrivate.
910 @Returns: the corresponding pointer.
911
912
913 <!-- ##### FUNCTION g_static_private_set ##### -->
914 <para>
915 Sets the pointer keyed to @private_key for the current thread and the
916 function @notify to be called with that pointer (NULL or non-NULL),
917 whenever the pointer is set again or whenever the current thread ends.
918 </para>
919
920 <para>
921 This function also works, if g_thread_init() has not yet been
922 called. If g_thread_init() is called later, the @data keyed to
923 @private_key will be inherited only by the main thread, i.e. the one that
924 called g_thread_init().
925 </para>
926
927 <note>
928 <para>
929 The @notify is working quite differently from @destructor in
930 g_private_new().
931 </para>
932 </note>
933
934 @private_key: a #GStaticPrivate.
935 @data: the new pointer.
936 @notify: a function to be called with the pointer, whenever the
937 current thread ends or sets this pointer again.
938
939