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