Added missing </para> Added missing </para> Added missing </para> Added
[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 <example>
211 <title>A function which will not work in a threaded environment</title>
212 <programlisting>
213   int give_me_next_number ()
214   {
215     static int current_number = 0;
216
217     /* now do a very complicated calculation to calculate the new number,
218        this might for example be a random number generator */
219     current_number = calc_next_number (current_number); 
220     return current_number;
221   }
222 </programlisting>
223 </example>
224 </para>
225
226 <para>
227 It is easy to see, that this won't work in a multithreaded
228 application. There current_number must be protected against shared
229 access. A first naive implementation would be:
230 </para>
231
232 <para>
233 <example>
234 <title>The wrong way to write a thread-safe function</title>
235 <programlisting>
236   int give_me_next_number ()
237   {
238     static int current_number = 0;
239     int ret_val;
240     static GMutex * mutex = NULL;
241
242     if (!mutex)
243       mutex = g_mutex_new ();
244     g_mutex_lock (mutex);
245     ret_val = current_number = calc_next_number (current_number); 
246     g_mutex_unlock (mutex);
247     return ret_val;
248   }
249 </programlisting>
250 </example>
251 </para>
252
253 <para>
254 This looks like it would work, but there is a race condition while
255 constructing the mutex and this code can't work reliable. So please do
256 not use such constructs in your own programs. One working solution is:
257 </para>
258
259 <para>
260 <example>
261 <title>A correct thread-safe function</title>
262 <programlisting>
263   static GMutex *give_me_next_number_mutex = NULL;
264
265   /* this function must be called before any call to give_me_next_number ()
266      it must be called exactly once. */
267   void init_give_me_next_number () 
268   {
269     g_assert (give_me_next_number_mutex == NULL);
270     give_me_next_number_mutex = g_mutex_new ();
271   }
272
273   int give_me_next_number ()
274   {
275     static int current_number = 0;
276     int ret_val;
277
278     g_mutex_lock (give_me_next_number_mutex);
279     ret_val = current_number = calc_next_number (current_number); 
280     g_mutex_unlock (give_me_next_number_mutex);
281     return ret_val;
282   }
283 </programlisting>
284 </example>
285 </para>
286
287 <para>
288 #GStaticMutex provides a simpler and safer way of doing this.
289 </para>
290
291 <para>
292 A #GMutex should only be accessed via the following functions.
293 </para>
294
295 <note>
296 <para>
297 All of the g_mutex_* functions are actually macros. Apart from taking
298 the addresses of them, you can however use them as if they were functions.
299 </para>
300 </note>
301
302
303 <!-- ##### MACRO g_mutex_new ##### -->
304
305 <para>
306 Creates a new #GMutex. 
307 </para>
308
309 <note>
310 <para>
311 This function will abort, if g_thread_init() has not been called yet.
312 </para>
313 </note>
314
315 @Returns: a new #GMutex.
316
317
318 <!-- ##### MACRO g_mutex_lock ##### -->
319
320 <para>
321 Locks the #GMutex. If the #GMutex is already locked by another thread,
322 the current thread will block until the #GMutex is unlocked by the
323 other thread.
324 </para>
325
326 <para>
327 This function can also be used, if g_thread_init() has not yet been
328 called and will do nothing then.
329 </para>
330
331 <note>
332 <para>
333 #GMutex is not guaranteed to be recursive, i.e. a thread might block,
334 if it already has locked the #GMutex. It will deadlock then, of
335 course.
336 </para>
337 </note>
338
339 @mutex: a #GMutex.
340
341
342 <!-- ##### MACRO g_mutex_trylock ##### -->
343
344 <para>
345 Tries to lock the #GMutex. If the #GMutex is already locked by another
346 thread, it immediately returns FALSE. Otherwise it locks the #GMutex
347 and returns TRUE.
348 </para>
349
350 <para>
351 This function can also be used, if g_thread_init() has not yet been
352 called and will immediately return TRUE then.
353 </para>
354
355 @mutex: a #GMutex.
356 @Returns: TRUE, if the #GMutex could be locked.
357
358
359 <!-- ##### MACRO g_mutex_unlock ##### -->
360
361 <para>
362 Unlocks the #GMutex. If another thread is blocked in a g_mutex_lock()
363 call, it will be woken and can lock the #GMutex itself. This function
364 can also be used, if g_thread_init() has not yet been called and will
365 do nothing then.
366 </para>
367
368 @mutex: a #GMutex.
369
370
371 <!-- ##### MACRO g_mutex_free ##### -->
372
373 <para>
374 Destroys the #GMutex.
375 </para>
376
377 @mutex: a #GMutex.
378
379
380 <!-- ##### STRUCT GStaticMutex ##### -->
381
382 <para>
383 A #GStaticMutex works like a #GMutex, but it has one significant
384 advantage. It doesn't need to be created at run-time like a #GMutex,
385 but can be defined at compile-time. Here is a shorter, easier and
386 safer version of our give_me_next_number() example:
387 </para>
388
389 <para>
390 <example>
391 <title>Using GStaticMutex to simplify thread-safe programming</title>
392 <programlisting>
393   int give_me_next_number ()
394   {
395     static int current_number = 0;
396     int ret_val;
397     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
398
399     g_static_mutex_lock (&amp;mutex);
400     ret_val = current_number = calc_next_number (current_number); 
401     g_static_mutex_unlock (&amp;mutex);
402     return ret_val;
403   }
404 </programlisting>
405 </example>
406 </para>
407
408 <para>
409 Even though #GStaticMutex is not opaque, it should only be used with
410 the following functions, as it is defined differently on different
411 platforms.
412 </para>
413
414 <para>All of the g_static_mutex_* functions can also be used, if
415 g_thread_init() has not yet.
416 </para>
417
418 <note>
419 <para>
420 All of the g_static_mutex_* functions are actually macros. Apart from
421 taking the addresses of them, you can however use them as if they were
422 functions.
423 </para>
424 </note>
425
426 @runtime_mutex: 
427
428 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
429
430 <para>
431 Every #GStaticMutex must be initialized with this macro, before it can
432 be used.
433 </para>
434
435 <para>
436 <example>
437 <title>Initializing a GStaticMutext</title>
438 <programlisting>
439 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
440 </programlisting>
441 </example>
442 </para>
443
444
445
446 <!-- ##### MACRO g_static_mutex_lock ##### -->
447 <para>
448 works like g_mutex_lock(), but for a #GStaticMutex.
449 </para>
450
451 @mutex: a #GStaticMutex.
452
453
454 <!-- ##### MACRO g_static_mutex_trylock ##### -->
455
456 <para>
457 works like g_mutex_trylock(), but for a #GStaticMutex.
458 </para>
459
460 @mutex: a #GStaticMutex.
461 @Returns: TRUE, if the #GStaticMutex could be locked.
462
463
464 <!-- ##### MACRO g_static_mutex_unlock ##### -->
465
466 <para>
467 works like g_mutex_unlock(), but for a #GStaticMutex.
468 </para>
469
470 @mutex: a #GStaticMutex.
471
472
473 <!-- ##### MACRO g_static_mutex_get_mutex ##### -->
474
475 <para>
476 For some operations (like g_cond_wait()) you must have a #GMutex
477 instead of a #GStaticMutex. This function will return the
478 corresponding #GMutex for every #GStaticMutex.
479 </para>
480
481 @mutex: a #GStaticMutex.
482 @Returns: the corresponding #GMutex.
483
484
485 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
486
487 <para>
488 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
489 with the advantage that they will expand to nothing in programs
490 compiled against a thread-disabled GLib, saving code and memory
491 there. #G_LOCK_DEFINE defines a lock. It can occur, where variable
492 definitions may occur in programs, i.e. in the first block of a
493 function or outside of functions. The @name parameter will be mangled
494 to get the name of the #GStaticMutex. This means, that you can use
495 names of existing variables as the parameter, e.g. the name of the
496 variable you intent to protect with the lock. Look at our
497 give_me_next_number() example using the G_LOCK_* macros:
498 </para>
499
500 <para>
501 <example>
502 <title>Using the G_LOCK_* convenience macros</title>
503 <programlisting>
504 int give_me_next_number ()
505   {
506     static int current_number = 0;
507     int ret_val;
508     G_LOCK_DEFINE_STATIC (current_number);
509
510     G_LOCK (current_number);
511     ret_val = current_number = calc_next_number (current_number); 
512     G_UNLOCK (current_number);
513     return ret_val;
514   }
515 </programlisting>
516 </example>
517 </para>
518
519 @name: the name of the lock.
520
521
522 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
523
524 <para>
525 This works like #G_LOCK_DEFINE, but it creates a static object.
526 </para>
527
528 @name: the name of the lock.
529
530
531 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
532
533 <para>
534 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
535 </para>
536
537 @name: the name of the lock.
538
539
540 <!-- ##### MACRO G_LOCK ##### -->
541
542 <para>
543 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
544 </para>
545
546 @name: the name of the lock.
547
548
549 <!-- ##### MACRO G_TRYLOCK ##### -->
550
551 <para>
552 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
553 </para>
554
555 @name: the name of the lock.
556 @Returns: TRUE, if the lock could be locked.
557
558
559 <!-- ##### MACRO G_UNLOCK ##### -->
560
561 <para>
562 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
563 </para>
564
565 @name: the name of the lock.
566
567
568 <!-- ##### STRUCT GCond ##### -->
569
570 <para>
571 The #GCond struct is an opaque data structure to represent a
572 condition. A #GCond is an object, that threads can block on, if they
573 find a certain condition to be false. If other threads change the
574 state of this condition they can signal the #GCond, such that the
575 waiting thread is woken up. 
576 </para>
577
578 <para>
579 <example>
580 <title>Using GCond to block a thread until a condition is satisfied</title>
581 <programlisting>
582 GCond* data_cond = NULL;   /* Must be initialized somewhere */
583 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
584 gpointer current_data = NULL;
585
586 void push_data (gpointer data)
587 {
588   g_mutex_lock (data_mutex);
589   current_data = data;
590   g_cond_signal (data_cond);
591   g_mutex_unlock (data_mutex);
592 }
593
594 gpointer pop_data ()
595 {
596   gpointer data;
597
598   g_mutex_lock (data_mutex);
599   while (!current_data)
600       g_cond_wait (data_cond, data_mutex);
601   data = current_data;
602   current_data = NULL;
603   g_mutex_unlock (data_mutex);
604   return data;
605 }
606 </programlisting>
607 </example>
608 </para>
609
610 <para>
611 Whenever a thread calls pop_data() now, it will wait until
612 current_data is non-NULL, i.e. until some other thread has called
613 push_data().
614 </para>
615
616 <note>
617 <para>
618 It is important to use the g_cond_wait() and g_cond_timed_wait()
619 functions only inside a loop, which checks for the condition to be
620 true as it is not guaranteed that the waiting thread will find it
621 fulfilled, even if the signaling thread left the condition
622 in that state. This is because another thread can have altered the
623 condition, before the waiting thread got the chance to be woken up,
624 even if the condition itself is protected by a #GMutex, like above.
625 </para>
626 </note>
627
628 <para>
629 A #GCond should only be accessed via the following functions.
630 </para>
631
632 <note>
633 <para>
634 All of the g_cond_* functions are actually macros. Apart from taking
635 the addresses of them, you can however use them as if they were functions.
636 </para>
637 </note>
638
639
640 <!-- ##### MACRO g_cond_new ##### -->
641
642 <para>
643 Creates a new #GCond. This function will abort, if g_thread_init()
644 has not been called yet.
645 </para>
646
647 @Returns: a new #GCond.
648
649
650 <!-- ##### MACRO g_cond_signal ##### -->
651 <para>
652 If threads are waiting for @cond, exactly one of them is woken up. It
653 is good practice to hold the same lock as the waiting thread, while
654 calling this function, though not required.
655 </para>
656
657 <para>
658 This function can also be used, if g_thread_init() has
659 not yet been called and will do nothing then.
660 </para>
661
662 @cond: a #GCond.
663
664
665 <!-- ##### MACRO g_cond_broadcast ##### -->
666
667 <para>
668 If threads are waiting for @cond, all of them are woken up. It is good
669 practice to lock the same mutex as the waiting threads, while calling
670 this function, though not required.
671 </para>
672
673 <para>
674 This function can also be used, if g_thread_init() has
675 not yet been called and will do nothing then.
676 </para>
677
678 @cond: a #GCond.
679
680
681 <!-- ##### MACRO g_cond_wait ##### -->
682
683 <para>
684 Waits until this thread is woken up on the #GCond. The #GMutex is
685 unlocked before falling asleep and locked again before resuming.
686 </para>
687
688 <para>
689 This function can also be used, if g_thread_init() has not yet been
690 called and will immediately return then.
691 </para>
692
693 @cond: a #GCond.
694 @mutex: the #GMutex, that is currently locked.
695
696
697 <!-- ##### MACRO g_cond_timed_wait ##### -->
698
699 <para>
700 Waits until this thread is woken up on the #GCond, but not longer than
701 until the time, that is specified by @abs_time. The #GMutex is
702 unlocked before falling asleep and locked again before resuming.
703 </para>
704
705 <para>
706 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
707 </para>
708
709 <para>
710 This function can also be used, if g_thread_init() has not yet been
711 called and will immediately return TRUE then.
712 </para>
713
714 @cond: a #GCond.
715 @mutex: the #GMutex, that is currently locked.
716 @abs_time: a #GTimeVal, determining the final time.
717 @Returns: TRUE, if the thread is woken up in time.
718
719
720 <!-- ##### MACRO g_cond_free ##### -->
721
722 <para>
723 Destroys the #GCond.
724 </para>
725
726 @cond: a #GCond.
727
728
729 <!-- ##### STRUCT GPrivate ##### -->
730 <para>
731 The #GPrivate struct is an opaque data structure to represent a thread
732 private data key. Threads can thereby obtain and set a pointer, which
733 is private to the current thread. Take our give_me_next_number()
734 example from above.  Now we don't want current_number to be shared
735 between the threads, but to be private to each thread. This can be
736 done as follows:
737
738 <example>
739 <title>Using GPrivate for per-thread data</title>
740 <programlisting>
741   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
742                                        /* with g_private_new (g_free); */
743
744   int give_me_next_number ()
745   {
746     int *current_number = g_private_get (current_number_key);
747
748     if (!current_number)
749     {
750       current_number = g_new (int,1);
751       *current_number = 0;
752       g_private_set (current_number_key, current_number);
753     }
754     *current_number = calc_next_number (*current_number); 
755     return *current_number;
756   }
757 </programlisting>
758 </example>
759 </para>
760
761 <para>
762 Here the pointer belonging to the key current_number_key is read. If
763 it is NULL, it has not been set yet. Then get memory for an integer
764 value, assign this memory to the pointer and write the pointer
765 back. Now we have an integer value, that is private to the current
766 thread.
767 </para>
768
769 <para>
770 The #GPrivate struct should only be accessed via the following functions.
771 </para>
772
773 <note>
774 <para>
775 All of the g_private_* functions are actually macros. Apart from taking
776 the addresses of them, you can however use them as if they were functions.
777 </para>
778 </note>
779
780
781 <!-- ##### MACRO g_private_new ##### -->
782
783 <para>
784 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
785 to a destructor function. Whenever a thread ends and the corresponding
786 pointer keyed to this instance of #GPrivate is non-NULL, the
787 destructor is called with this pointer as the argument.
788 </para>
789
790 <note>
791 <para>
792 The @destructor is working quite differently from @notify in
793 g_static_private_set().
794 </para>
795 </note>
796
797 <note>
798 <para>
799 A #GPrivate can not be destroyed. Reuse it instead, if you can to
800 avoid shortage.
801 </para>
802 </note>
803
804 <note>
805 <para>
806 This function will abort, if g_thread_init() has not been called yet.
807 </para>
808 </note>
809
810 @destructor: a function to handle the data keyed to #GPrivate, when a
811 thread ends.
812
813
814 <!-- ##### MACRO g_private_get ##### -->
815
816 <para>
817 Returns the pointer keyed to @private_key for the current thread. This
818 pointer is NULL, when g_private_set() hasn't been called for the
819 current @private_key and thread yet.
820 </para>
821
822 <para>
823 This function can also be used, if g_thread_init() has not yet been
824 called and will return the value of @private_key casted to #gpointer then.
825 </para>
826
827 @private_key: a #GPrivate.
828 @Returns: the corresponding pointer.
829
830
831 <!-- ##### MACRO g_private_set ##### -->
832
833 <para>
834 Sets the pointer keyed to @private_key for the current thread.
835 </para>
836
837 <para>
838 This function can also be used, if g_thread_init() has not yet been
839 called and will set @private_key to @data casted to #GPrivate* then.
840 </para>
841
842 @private_key: a #GPrivate.
843 @value: 
844 <!-- # Unused Parameters # -->
845 @data: the new pointer.
846
847
848 <!-- ##### STRUCT GStaticPrivate ##### -->
849
850 <para>
851 A #GStaticPrivate works almost like a #GPrivate, but it has one
852 significant advantage. It doesn't need to be created at run-time like
853 a #GPrivate, but can be defined at compile-time. This is similar to
854 the difference between #GMutex and #GStaticMutex. Now look at our
855 give_me_next_number() example with #GStaticPrivate:
856 </para>
857
858 <para>
859 <example>
860 <title>Using GStaticPrivate for per-thread data</title>
861 <programlisting>
862   int give_me_next_number ()
863   {
864     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
865     int *current_number = g_static_private_get (&amp;current_number_key);
866
867     if (!current_number)
868     {
869       current_number = g_new (int,1);
870       *current_number = 0;
871       g_static_private_set (&amp;current_number_key, current_number, g_free);
872     }
873     *current_number = calc_next_number (*current_number); 
874     return *current_number;
875   }
876 </programlisting>
877 </example>
878 </para>
879
880 @index: 
881
882 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
883 <para>
884 Every #GStaticPrivate must be initialized with this macro, before it can
885 be used.
886 </para>
887
888 <para>
889 <informalexample>
890 <programlisting>
891 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
892 </programlisting>
893 </informalexample>
894 </para>
895
896
897
898 <!-- ##### FUNCTION g_static_private_get ##### -->
899 <para>
900 Works like g_private_get() only for a #GStaticPrivate.
901 </para>
902
903 <para>
904 This function also works, if g_thread_init() has not yet been called.
905 </para>
906
907 @private_key: a #GStaticPrivate.
908 @Returns: the corresponding pointer.
909
910
911 <!-- ##### FUNCTION g_static_private_set ##### -->
912 <para>
913 Sets the pointer keyed to @private_key for the current thread and the
914 function @notify to be called with that pointer (NULL or non-NULL),
915 whenever the pointer is set again or whenever the current thread ends.
916 </para>
917
918 <para>
919 This function also works, if g_thread_init() has not yet been
920 called. If g_thread_init() is called later, the @data keyed to
921 @private_key will be inherited only by the main thread, i.e. the one that
922 called g_thread_init().
923 </para>
924
925 <note>
926 <para>
927 The @notify is working quite differently from @destructor in
928 g_private_new().
929 </para>
930 </note>
931
932 @private_key: a #GStaticPrivate.
933 @data: the new pointer.
934 @notify: a function to be called with the pointer, whenever the
935 current thread ends or sets this pointer again.
936
937