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