68889f4e904ee07a5cb44e9e39c91c51061c03e8
[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 <!-- ##### MACRO G_THREAD_ERROR ##### -->
85 <para>
86
87 </para>
88
89
90
91 <!-- ##### ENUM GThreadError ##### -->
92 <para>
93
94 </para>
95
96 @G_THREAD_ERROR_AGAIN: 
97
98 <!-- ##### STRUCT GThreadFunctions ##### -->
99
100 <para>
101 This function table is used by g_thread_init() to initialize the
102 thread system. The functions in that table are directly used by their
103 g_* prepended counterparts, that are described here, e.g. if you call
104 g_mutex_new() then mutex_new() from the table provided to
105 g_thread_init() will be called.
106 </para>
107
108 <note>
109 <para>
110 This struct should only be used, if you know, what you are doing.
111 </para>
112 </note>
113
114 @mutex_new: 
115 @mutex_lock: 
116 @mutex_trylock: 
117 @mutex_unlock: 
118 @mutex_free: 
119 @cond_new: 
120 @cond_signal: 
121 @cond_broadcast: 
122 @cond_wait: 
123 @cond_timed_wait: 
124 @cond_free: 
125 @private_new: 
126 @private_get: 
127 @private_set: 
128 @thread_create: 
129 @thread_yield: 
130 @thread_join: 
131 @thread_exit: 
132 @thread_set_priority: 
133 @thread_self: 
134
135 <!-- ##### FUNCTION g_thread_init ##### -->
136
137 <para>
138 Before you use a thread related function in GLib, you should
139 initialize the thread system. This is done by calling
140 g_thread_init(). Most of the time you will only have to call
141 g_thread_init(NULL). 
142 </para>
143
144 <note>
145 <para>
146 You should only call g_thread_init() with a non-NULL parameter, if you
147 really know, what you are doing.
148 </para>
149 </note>
150
151 <note>
152 <para>
153 g_thread_init() must not be called directly or indirectly as a
154 callback from GLib.
155 </para>
156 </note>
157
158 <para>
159 g_thread_init() might only be called once. On the second call
160 it will abort with an error. If you want to make sure, that the thread
161 system is initialized, you can do that too:
162 </para>
163
164 <para>
165 <informalexample>
166 <programlisting>
167 if (!g_thread_supported ()) g_thread_init (NULL);
168 </programlisting>
169 </informalexample>
170 </para>
171
172 <para>
173 After that line either the thread system is initialized or the program
174 will abort, if no thread system is available in GLib, i.e. either
175 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
176 </para>
177
178 <para>
179 If no thread system is available and @vtable is NULL or if not all
180 elements of @vtable are non-NULL, then g_thread_init() will abort.
181 </para>
182
183 <note>
184 <para>
185 To use g_thread_init() in your program, you have to link with the
186 libraries, that the command "glib-config --libs gthread" outputs. This
187 is not the case for all the other thread related functions of
188 GLib. Those can be used without having to link with the thread
189 libraries.
190 </para>
191 </note>
192
193 @vtable: a function table of type #GThreadFunctions, that provides the
194 entry points to the thread system to be used.
195
196
197 <!-- ##### FUNCTION g_thread_supported ##### -->
198 <para>
199 This function returns, whether the thread system is initialized or
200 not.
201 </para>
202
203 <note>
204 <para>
205 This function is actually a macro. Apart from taking the address of it
206 you can however use it as if it was a function.
207 </para>
208 </note>
209
210 @Returns: TRUE, if the thread system is initialized.
211
212
213 <!-- ##### USER_FUNCTION GThreadFunc ##### -->
214 <para>
215
216 </para>
217
218 @value: 
219
220
221 <!-- ##### ENUM GThreadPriority ##### -->
222 <para>
223
224 </para>
225
226 @G_THREAD_PRIORITY_LOW: 
227 @G_THREAD_PRIORITY_NORMAL: 
228 @G_THREAD_PRIORITY_HIGH: 
229 @G_THREAD_PRIORITY_URGENT: 
230
231 <!-- ##### STRUCT GThread ##### -->
232 <para>
233
234 </para>
235
236 @priority: 
237 @bound: 
238 @joinable: 
239
240 <!-- ##### FUNCTION g_thread_create ##### -->
241 <para>
242
243 </para>
244
245 @thread_func: 
246 @arg: 
247 @stack_size: 
248 @joinable: 
249 @bound: 
250 @priority: 
251 @error: 
252 @Returns: 
253
254
255 <!-- ##### FUNCTION g_thread_self ##### -->
256 <para>
257
258 </para>
259
260 @Returns: 
261
262
263 <!-- ##### FUNCTION g_thread_join ##### -->
264 <para>
265
266 </para>
267
268 @thread: 
269
270
271 <!-- ##### FUNCTION g_thread_set_priority ##### -->
272 <para>
273
274 </para>
275
276 @thread: 
277 @priority: 
278
279
280 <!-- ##### MACRO g_thread_yield ##### -->
281 <para>
282
283 </para>
284
285
286
287 <!-- ##### MACRO g_thread_exit ##### -->
288 <para>
289
290 </para>
291
292
293
294 <!-- ##### STRUCT GMutex ##### -->
295
296 <para>
297 The #GMutex struct is an opaque data structure to represent a mutex
298 (mutual exclusion). It can be used to protect data against shared
299 access. Take for example the following function:
300
301 <example>
302 <title>A function which will not work in a threaded environment</title>
303 <programlisting>
304   int give_me_next_number ()
305   {
306     static int current_number = 0;
307
308     /* now do a very complicated calculation to calculate the new number,
309        this might for example be a random number generator */
310     current_number = calc_next_number (current_number); 
311     return current_number;
312   }
313 </programlisting>
314 </example>
315 </para>
316
317 <para>
318 It is easy to see, that this won't work in a multithreaded
319 application. There current_number must be protected against shared
320 access. A first naive implementation would be:
321 </para>
322
323 <para>
324 <example>
325 <title>The wrong way to write a thread-safe function</title>
326 <programlisting>
327   int give_me_next_number ()
328   {
329     static int current_number = 0;
330     int ret_val;
331     static GMutex * mutex = NULL;
332
333     if (!mutex)
334       mutex = g_mutex_new ();
335     g_mutex_lock (mutex);
336     ret_val = current_number = calc_next_number (current_number); 
337     g_mutex_unlock (mutex);
338     return ret_val;
339   }
340 </programlisting>
341 </example>
342 </para>
343
344 <para>
345 This looks like it would work, but there is a race condition while
346 constructing the mutex and this code can't work reliable. So please do
347 not use such constructs in your own programs. One working solution is:
348 </para>
349
350 <para>
351 <example>
352 <title>A correct thread-safe function</title>
353 <programlisting>
354   static GMutex *give_me_next_number_mutex = NULL;
355
356   /* this function must be called before any call to give_me_next_number ()
357      it must be called exactly once. */
358   void init_give_me_next_number () 
359   {
360     g_assert (give_me_next_number_mutex == NULL);
361     give_me_next_number_mutex = g_mutex_new ();
362   }
363
364   int give_me_next_number ()
365   {
366     static int current_number = 0;
367     int ret_val;
368
369     g_mutex_lock (give_me_next_number_mutex);
370     ret_val = current_number = calc_next_number (current_number); 
371     g_mutex_unlock (give_me_next_number_mutex);
372     return ret_val;
373   }
374 </programlisting>
375 </example>
376 </para>
377
378 <para>
379 #GStaticMutex provides a simpler and safer way of doing this.
380 </para>
381
382 <para>
383 A #GMutex should only be accessed via the following functions.
384 </para>
385
386 <note>
387 <para>
388 All of the g_mutex_* functions are actually macros. Apart from taking
389 the addresses of them, you can however use them as if they were functions.
390 </para>
391 </note>
392
393
394 <!-- ##### FUNCTION g_mutex_new ##### -->
395
396 <para>
397 Creates a new #GMutex. 
398 </para>
399
400 <note>
401 <para>
402 This function will abort, if g_thread_init() has not been called yet.
403 </para>
404 </note>
405
406 @Returns: a new #GMutex.
407
408
409 <!-- ##### FUNCTION g_mutex_lock ##### -->
410
411 <para>
412 Locks the #GMutex. If the #GMutex is already locked by another thread,
413 the current thread will block until the #GMutex is unlocked by the
414 other thread.
415 </para>
416
417 <para>
418 This function can also be used, if g_thread_init() has not yet been
419 called and will do nothing then.
420 </para>
421
422 <note>
423 <para>
424 #GMutex is not guaranteed to be recursive, i.e. a thread might block,
425 if it already has locked the #GMutex. It will deadlock then, of
426 course.
427 </para>
428 </note>
429
430 @mutex: a #GMutex.
431
432
433 <!-- ##### FUNCTION g_mutex_trylock ##### -->
434
435 <para>
436 Tries to lock the #GMutex. If the #GMutex is already locked by another
437 thread, it immediately returns FALSE. Otherwise it locks the #GMutex
438 and returns TRUE.
439 </para>
440
441 <para>
442 This function can also be used, if g_thread_init() has not yet been
443 called and will immediately return TRUE then.
444 </para>
445
446 @mutex: a #GMutex.
447 @Returns: TRUE, if the #GMutex could be locked.
448
449
450 <!-- ##### FUNCTION g_mutex_unlock ##### -->
451
452 <para>
453 Unlocks the #GMutex. If another thread is blocked in a g_mutex_lock()
454 call, it will be woken and can lock the #GMutex itself. This function
455 can also be used, if g_thread_init() has not yet been called and will
456 do nothing then.
457 </para>
458
459 @mutex: a #GMutex.
460
461
462 <!-- ##### FUNCTION g_mutex_free ##### -->
463
464 <para>
465 Destroys the #GMutex.
466 </para>
467
468 @mutex: a #GMutex.
469
470
471 <!-- ##### STRUCT GStaticMutex ##### -->
472
473 <para>
474 A #GStaticMutex works like a #GMutex, but it has one significant
475 advantage. It doesn't need to be created at run-time like a #GMutex,
476 but can be defined at compile-time. Here is a shorter, easier and
477 safer version of our give_me_next_number() example:
478 </para>
479
480 <para>
481 <example>
482 <title>Using GStaticMutex to simplify thread-safe programming</title>
483 <programlisting>
484   int give_me_next_number ()
485   {
486     static int current_number = 0;
487     int ret_val;
488     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
489
490     g_static_mutex_lock (&amp;mutex);
491     ret_val = current_number = calc_next_number (current_number); 
492     g_static_mutex_unlock (&amp;mutex);
493     return ret_val;
494   }
495 </programlisting>
496 </example>
497 </para>
498
499 <para>
500 Even though #GStaticMutex is not opaque, it should only be used with
501 the following functions, as it is defined differently on different
502 platforms.
503 </para>
504
505 <para>All of the g_static_mutex_* functions can also be used, if
506 g_thread_init() has not yet.
507 </para>
508
509 <note>
510 <para>
511 All of the g_static_mutex_* functions are actually macros. Apart from
512 taking the addresses of them, you can however use them as if they were
513 functions.
514 </para>
515 </note>
516
517
518 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
519
520 <para>
521 Every #GStaticMutex must be initialized with this macro, before it can
522 be used.
523 </para>
524
525 <para>
526 <example>
527 <title>Initializing a GStaticMutext</title>
528 <programlisting>
529 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
530 </programlisting>
531 </example>
532 </para>
533
534
535
536 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
537 <para>
538 works like g_mutex_lock(), but for a #GStaticMutex.
539 </para>
540
541 @mutex: a #GStaticMutex.
542
543
544 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
545
546 <para>
547 works like g_mutex_trylock(), but for a #GStaticMutex.
548 </para>
549
550 @mutex: a #GStaticMutex.
551 @Returns: TRUE, if the #GStaticMutex could be locked.
552
553
554 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
555
556 <para>
557 works like g_mutex_unlock(), but for a #GStaticMutex.
558 </para>
559
560 @mutex: a #GStaticMutex.
561
562
563 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
564
565 <para>
566 For some operations (like g_cond_wait()) you must have a #GMutex
567 instead of a #GStaticMutex. This function will return the
568 corresponding #GMutex for every #GStaticMutex.
569 </para>
570
571 @mutex: a #GStaticMutex.
572 @Returns: the corresponding #GMutex.
573
574
575 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
576
577 <para>
578 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
579 with the advantage that they will expand to nothing in programs
580 compiled against a thread-disabled GLib, saving code and memory
581 there. #G_LOCK_DEFINE defines a lock. It can occur, where variable
582 definitions may occur in programs, i.e. in the first block of a
583 function or outside of functions. The @name parameter will be mangled
584 to get the name of the #GStaticMutex. This means, that you can use
585 names of existing variables as the parameter, e.g. the name of the
586 variable you intent to protect with the lock. Look at our
587 give_me_next_number() example using the G_LOCK_* macros:
588 </para>
589
590 <para>
591 <example>
592 <title>Using the G_LOCK_* convenience macros</title>
593 <programlisting>
594 G_LOCK_DEFINE (current_number);
595
596 int give_me_next_number ()
597   {
598     static int current_number = 0;
599     int ret_val;
600
601     G_LOCK (current_number);
602     ret_val = current_number = calc_next_number (current_number); 
603     G_UNLOCK (current_number);
604     return ret_val;
605   }
606 </programlisting>
607 </example>
608 </para>
609
610 @name: the name of the lock.
611
612
613 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
614
615 <para>
616 This works like #G_LOCK_DEFINE, but it creates a static object.
617 </para>
618
619 @name: the name of the lock.
620
621
622 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
623
624 <para>
625 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
626 </para>
627
628 @name: the name of the lock.
629
630
631 <!-- ##### MACRO G_LOCK ##### -->
632
633 <para>
634 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
635 </para>
636
637 @name: the name of the lock.
638
639
640 <!-- ##### MACRO G_TRYLOCK ##### -->
641
642 <para>
643 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
644 </para>
645
646 @name: the name of the lock.
647 @Returns: TRUE, if the lock could be locked.
648
649
650 <!-- ##### MACRO G_UNLOCK ##### -->
651
652 <para>
653 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
654 </para>
655
656 @name: the name of the lock.
657
658
659 <!-- ##### STRUCT GStaticRecMutex ##### -->
660 <para>
661
662 </para>
663
664 @mutex: 
665 @depth: 
666 @owner: 
667
668 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
669 <para>
670
671 </para>
672
673
674
675 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
676 <para>
677
678 </para>
679
680 @mutex: 
681
682
683 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
684 <para>
685
686 </para>
687
688 @mutex: 
689 @Returns: 
690
691
692 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
693 <para>
694
695 </para>
696
697 @mutex: 
698
699
700 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
701 <para>
702
703 </para>
704
705 @mutex: 
706 @depth: 
707
708
709 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
710 <para>
711
712 </para>
713
714 @mutex: 
715 @Returns: 
716
717
718 <!-- ##### STRUCT GStaticRWLock ##### -->
719 <para>
720
721 </para>
722
723 @mutex: 
724 @read_cond: 
725 @write_cond: 
726 @read_counter: 
727 @write: 
728 @want_to_write: 
729
730 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
731 <para>
732
733 </para>
734
735
736
737 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
738 <para>
739
740 </para>
741
742 @lock: 
743
744
745 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
746 <para>
747
748 </para>
749
750 @lock: 
751 @Returns: 
752
753
754 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
755 <para>
756
757 </para>
758
759 @lock: 
760
761
762 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
763 <para>
764
765 </para>
766
767 @lock: 
768
769
770 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
771 <para>
772
773 </para>
774
775 @lock: 
776 @Returns: 
777
778
779 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
780 <para>
781
782 </para>
783
784 @lock: 
785
786
787 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
788 <para>
789
790 </para>
791
792 @lock: 
793
794
795 <!-- ##### STRUCT GCond ##### -->
796
797 <para>
798 The #GCond struct is an opaque data structure to represent a
799 condition. A #GCond is an object, that threads can block on, if they
800 find a certain condition to be false. If other threads change the
801 state of this condition they can signal the #GCond, such that the
802 waiting thread is woken up. 
803 </para>
804
805 <para>
806 <example>
807 <title>Using GCond to block a thread until a condition is satisfied</title>
808 <programlisting>
809 GCond* data_cond = NULL;   /* Must be initialized somewhere */
810 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
811 gpointer current_data = NULL;
812
813 void push_data (gpointer data)
814 {
815   g_mutex_lock (data_mutex);
816   current_data = data;
817   g_cond_signal (data_cond);
818   g_mutex_unlock (data_mutex);
819 }
820
821 gpointer pop_data ()
822 {
823   gpointer data;
824
825   g_mutex_lock (data_mutex);
826   while (!current_data)
827       g_cond_wait (data_cond, data_mutex);
828   data = current_data;
829   current_data = NULL;
830   g_mutex_unlock (data_mutex);
831   return data;
832 }
833 </programlisting>
834 </example>
835 </para>
836
837 <para>
838 Whenever a thread calls pop_data() now, it will wait until
839 current_data is non-NULL, i.e. until some other thread has called
840 push_data().
841 </para>
842
843 <note>
844 <para>
845 It is important to use the g_cond_wait() and g_cond_timed_wait()
846 functions only inside a loop, which checks for the condition to be
847 true as it is not guaranteed that the waiting thread will find it
848 fulfilled, even if the signaling thread left the condition
849 in that state. This is because another thread can have altered the
850 condition, before the waiting thread got the chance to be woken up,
851 even if the condition itself is protected by a #GMutex, like above.
852 </para>
853 </note>
854
855 <para>
856 A #GCond should only be accessed via the following functions.
857 </para>
858
859 <note>
860 <para>
861 All of the g_cond_* functions are actually macros. Apart from taking
862 the addresses of them, you can however use them as if they were functions.
863 </para>
864 </note>
865
866
867 <!-- ##### FUNCTION g_cond_new ##### -->
868
869 <para>
870 Creates a new #GCond. This function will abort, if g_thread_init()
871 has not been called yet.
872 </para>
873
874 @Returns: a new #GCond.
875
876
877 <!-- ##### FUNCTION g_cond_signal ##### -->
878 <para>
879 If threads are waiting for @cond, exactly one of them is woken up. It
880 is good practice to hold the same lock as the waiting thread, while
881 calling this function, though not required.
882 </para>
883
884 <para>
885 This function can also be used, if g_thread_init() has
886 not yet been called and will do nothing then.
887 </para>
888
889 @cond: a #GCond.
890
891
892 <!-- ##### FUNCTION g_cond_broadcast ##### -->
893
894 <para>
895 If threads are waiting for @cond, all of them are woken up. It is good
896 practice to lock the same mutex as the waiting threads, while calling
897 this function, though not required.
898 </para>
899
900 <para>
901 This function can also be used, if g_thread_init() has
902 not yet been called and will do nothing then.
903 </para>
904
905 @cond: a #GCond.
906
907
908 <!-- ##### FUNCTION g_cond_wait ##### -->
909
910 <para>
911 Waits until this thread is woken up on the #GCond. The #GMutex is
912 unlocked before falling asleep and locked again before resuming.
913 </para>
914
915 <para>
916 This function can also be used, if g_thread_init() has not yet been
917 called and will immediately return then.
918 </para>
919
920 @cond: a #GCond.
921 @mutex: the #GMutex, that is currently locked.
922
923
924 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
925
926 <para>
927 Waits until this thread is woken up on the #GCond, but not longer than
928 until the time, that is specified by @abs_time. The #GMutex is
929 unlocked before falling asleep and locked again before resuming.
930 </para>
931
932 <para>
933 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
934 </para>
935
936 <para>
937 This function can also be used, if g_thread_init() has not yet been
938 called and will immediately return TRUE then.
939 </para>
940
941 @cond: a #GCond.
942 @mutex: the #GMutex, that is currently locked.
943 @abs_time: a #GTimeVal, determining the final time.
944 @Returns: TRUE, if the thread is woken up in time.
945
946
947 <!-- ##### FUNCTION g_cond_free ##### -->
948
949 <para>
950 Destroys the #GCond.
951 </para>
952
953 @cond: a #GCond.
954
955
956 <!-- ##### STRUCT GPrivate ##### -->
957 <para>
958 The #GPrivate struct is an opaque data structure to represent a thread
959 private data key. Threads can thereby obtain and set a pointer, which
960 is private to the current thread. Take our give_me_next_number()
961 example from above.  Now we don't want current_number to be shared
962 between the threads, but to be private to each thread. This can be
963 done as follows:
964
965 <example>
966 <title>Using GPrivate for per-thread data</title>
967 <programlisting>
968   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
969                                        /* with g_private_new (g_free); */
970
971   int give_me_next_number ()
972   {
973     int *current_number = g_private_get (current_number_key);
974
975     if (!current_number)
976     {
977       current_number = g_new (int,1);
978       *current_number = 0;
979       g_private_set (current_number_key, current_number);
980     }
981     *current_number = calc_next_number (*current_number); 
982     return *current_number;
983   }
984 </programlisting>
985 </example>
986 </para>
987
988 <para>
989 Here the pointer belonging to the key current_number_key is read. If
990 it is NULL, it has not been set yet. Then get memory for an integer
991 value, assign this memory to the pointer and write the pointer
992 back. Now we have an integer value, that is private to the current
993 thread.
994 </para>
995
996 <para>
997 The #GPrivate struct should only be accessed via the following functions.
998 </para>
999
1000 <note>
1001 <para>
1002 All of the g_private_* functions are actually macros. Apart from taking
1003 the addresses of them, you can however use them as if they were functions.
1004 </para>
1005 </note>
1006
1007
1008 <!-- ##### FUNCTION g_private_new ##### -->
1009
1010 <para>
1011 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1012 to a destructor function. Whenever a thread ends and the corresponding
1013 pointer keyed to this instance of #GPrivate is non-NULL, the
1014 destructor is called with this pointer as the argument.
1015 </para>
1016
1017 <note>
1018 <para>
1019 The @destructor is working quite differently from @notify in
1020 g_static_private_set().
1021 </para>
1022 </note>
1023
1024 <note>
1025 <para>
1026 A #GPrivate can not be destroyed. Reuse it instead, if you can to
1027 avoid shortage.
1028 </para>
1029 </note>
1030
1031 <note>
1032 <para>
1033 This function will abort, if g_thread_init() has not been called yet.
1034 </para>
1035 </note>
1036
1037 @destructor: a function to handle the data keyed to #GPrivate, when a
1038 thread ends.
1039 @Returns: 
1040
1041
1042 <!-- ##### FUNCTION g_private_get ##### -->
1043
1044 <para>
1045 Returns the pointer keyed to @private_key for the current thread. This
1046 pointer is NULL, when g_private_set() hasn't been called for the
1047 current @private_key and thread yet.
1048 </para>
1049
1050 <para>
1051 This function can also be used, if g_thread_init() has not yet been
1052 called and will return the value of @private_key casted to #gpointer then.
1053 </para>
1054
1055 @private_key: a #GPrivate.
1056 @Returns: the corresponding pointer.
1057
1058
1059 <!-- ##### FUNCTION g_private_set ##### -->
1060
1061 <para>
1062 Sets the pointer keyed to @private_key for the current thread.
1063 </para>
1064
1065 <para>
1066 This function can also be used, if g_thread_init() has not yet been
1067 called and will set @private_key to @data casted to #GPrivate* then.
1068 </para>
1069
1070 @private_key: a #GPrivate.
1071 @data: the new pointer.
1072 <!-- # Unused Parameters # -->
1073 @value: 
1074
1075
1076 <!-- ##### STRUCT GStaticPrivate ##### -->
1077
1078 <para>
1079 A #GStaticPrivate works almost like a #GPrivate, but it has one
1080 significant advantage. It doesn't need to be created at run-time like
1081 a #GPrivate, but can be defined at compile-time. This is similar to
1082 the difference between #GMutex and #GStaticMutex. Now look at our
1083 give_me_next_number() example with #GStaticPrivate:
1084 </para>
1085
1086 <para>
1087 <example>
1088 <title>Using GStaticPrivate for per-thread data</title>
1089 <programlisting>
1090   int give_me_next_number ()
1091   {
1092     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1093     int *current_number = g_static_private_get (&amp;current_number_key);
1094
1095     if (!current_number)
1096     {
1097       current_number = g_new (int,1);
1098       *current_number = 0;
1099       g_static_private_set (&amp;current_number_key, current_number, g_free);
1100     }
1101     *current_number = calc_next_number (*current_number); 
1102     return *current_number;
1103   }
1104 </programlisting>
1105 </example>
1106 </para>
1107
1108 @index: 
1109
1110 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1111 <para>
1112 Every #GStaticPrivate must be initialized with this macro, before it can
1113 be used.
1114 </para>
1115
1116 <para>
1117 <informalexample>
1118 <programlisting>
1119 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1120 </programlisting>
1121 </informalexample>
1122 </para>
1123
1124
1125
1126 <!-- ##### FUNCTION g_static_private_get ##### -->
1127 <para>
1128 Works like g_private_get() only for a #GStaticPrivate.
1129 </para>
1130
1131 <para>
1132 This function also works, if g_thread_init() has not yet been called.
1133 </para>
1134
1135 @private_key: a #GStaticPrivate.
1136 @Returns: the corresponding pointer.
1137
1138
1139 <!-- ##### FUNCTION g_static_private_get_for_thread ##### -->
1140 <para>
1141
1142 </para>
1143
1144 @private_key: 
1145 @thread: 
1146 @Returns: 
1147
1148
1149 <!-- ##### FUNCTION g_static_private_set ##### -->
1150 <para>
1151 Sets the pointer keyed to @private_key for the current thread and the
1152 function @notify to be called with that pointer (NULL or non-NULL),
1153 whenever the pointer is set again or whenever the current thread ends.
1154 </para>
1155
1156 <para>
1157 This function also works, if g_thread_init() has not yet been
1158 called. If g_thread_init() is called later, the @data keyed to
1159 @private_key will be inherited only by the main thread, i.e. the one that
1160 called g_thread_init().
1161 </para>
1162
1163 <note>
1164 <para>
1165 The @notify is working quite differently from @destructor in
1166 g_private_new().
1167 </para>
1168 </note>
1169
1170 @private_key: a #GStaticPrivate.
1171 @data: the new pointer.
1172 @notify: a function to be called with the pointer, whenever the
1173 current thread ends or sets this pointer again.
1174
1175
1176 <!-- ##### FUNCTION g_static_private_set_for_thread ##### -->
1177 <para>
1178
1179 </para>
1180
1181 @private_key: 
1182 @thread: 
1183 @data: 
1184 @notify: 
1185
1186