Updated.
[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 threads, different mutexes, conditions
8 and thread private data.
9
10 <!-- ##### SECTION Long_Description ##### -->
11
12 <para>
13 Threads act almost like processes, but unlike processes all threads of
14 one process share the same memory. This is good, as it provides easy
15 communication between the involved threads via this shared memory, and
16 it is bad, because strange things (so called Heisenbugs) might happen,
17 when the program is not carefully designed. Especially bad is, that due
18 to the concurrent nature of threads no assumptions on the order of
19 execution of different threads can be done unless explicitly forced by
20 the programmer through synchronization primitives.
21 </para>
22
23 <para>
24 The aim of the thread related functions in GLib is to provide a
25 portable means for writing multi-threaded software. There are
26 primitives for mutexes to protect the access to portions of memory
27 (#GMutex, #GStaticMutex, #G_LOCK_DEFINE, #GStaticRecMutex and
28 #GStaticRWLock), there are primitives for condition variables to allow
29 synchronization of threads (#GCond) and finally there are primitives
30 for thread-private data, that every thread has a private instance of
31 (#GPrivate, #GStaticPrivate). Last but definitely not least there are
32 primitives to portably create and manage threads (#GThread).
33 </para>
34
35 <!-- ##### SECTION See_Also ##### -->
36 <para>
37 <variablelist>
38
39 <varlistentry>
40 <term>#GThreadPool</term>
41 <listitem><para>Thread pools.</para></listitem>
42 </varlistentry>
43
44 <varlistentry>
45 <term>#GAsyncQueue</term>
46 <listitem><para>Send asynchronous messages between threads.</para></listitem>
47 </varlistentry>
48
49 </variablelist>
50 </para>
51
52 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
53
54 <para>
55 This macro is defined, if GLib was compiled with thread support. This
56 does not necessarily mean, that there is a thread implementation
57 available, but the infrastructure is in place and once you provide a
58 thread implementation to g_thread_init(), GLib will be multi-thread
59 safe. It isn't and cannot be, if #G_THREADS_ENABLED is not defined.
60 </para>
61
62
63
64 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
65
66 <para>
67 This macro is defined, if POSIX style threads are used.
68 </para>
69
70
71
72 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
73
74 <para>
75 This macro is defined, if the Solaris thread system is used.
76 </para>
77
78
79
80 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
81
82 <para>
83 This macro is defined, if no thread implementation is used. You can
84 however provide one to g_thread_init() to make GLib multi-thread safe.
85 </para>
86
87
88
89 <!-- ##### MACRO G_THREAD_ERROR ##### -->
90 <para>
91 The error domain of the GLib thread subsystem.
92 </para>
93
94
95
96 <!-- ##### ENUM GThreadError ##### -->
97 <para>
98 Possible errors of thread related functions.
99 </para>
100
101 @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
102 shortage. Try again later.
103
104 <!-- ##### STRUCT GThreadFunctions ##### -->
105
106 <para>
107 This function table is used by g_thread_init() to initialize the
108 thread system. The functions in that table are directly used by their
109 g_* prepended counterparts, that are described here, e.g. if you call
110 g_mutex_new() then mutex_new() from the table provided to
111 g_thread_init() will be called.
112 </para>
113
114 <note>
115 <para>
116 This struct should only be used, if you know, what you are doing.
117 </para>
118 </note>
119
120 @mutex_new: 
121 @mutex_lock: 
122 @mutex_trylock: 
123 @mutex_unlock: 
124 @mutex_free: 
125 @cond_new: 
126 @cond_signal: 
127 @cond_broadcast: 
128 @cond_wait: 
129 @cond_timed_wait: 
130 @cond_free: 
131 @private_new: 
132 @private_get: 
133 @private_set: 
134 @thread_create: 
135 @thread_yield: 
136 @thread_join: 
137 @thread_exit: 
138 @thread_set_priority: 
139 @thread_self: 
140
141 <!-- ##### FUNCTION g_thread_init ##### -->
142
143 <para>
144 Before you use a thread related function in GLib, you should
145 initialize the thread system. This is done by calling
146 g_thread_init(). Most of the time you will only have to call
147 g_thread_init(NULL). 
148 </para>
149
150 <note>
151 <para>
152 You should only call g_thread_init() with a non-NULL parameter, if you
153 really know, what you are doing.
154 </para>
155 </note>
156
157 <note>
158 <para>
159 g_thread_init() must not be called directly or indirectly as a
160 call-back from GLib. Also no mutexes may be currently locked, while
161 calling g_thread_init().
162 </para>
163 </note>
164
165 <para>
166 g_thread_init() might only be called once. On the second call
167 it will abort with an error. If you want to make sure, that the thread
168 system is initialized, you can do that too:
169 </para>
170
171 <para>
172 <informalexample>
173 <programlisting>
174 if (!g_thread_supported ()) g_thread_init (NULL);
175 </programlisting>
176 </informalexample>
177 </para>
178
179 <para>
180 After that line either the thread system is initialized or the program
181 will abort, if no thread system is available in GLib, i.e. either
182 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
183 </para>
184
185 <para>
186 If no thread system is available and @vtable is NULL or if not all
187 elements of @vtable are non-NULL, then g_thread_init() will abort.
188 </para>
189
190 <note>
191 <para>
192 To use g_thread_init() in your program, you have to link with the
193 libraries, that the command "glib-config --libs gthread" outputs. This
194 is not the case for all the other thread related functions of
195 GLib. Those can be used without having to link with the thread
196 libraries.
197 </para>
198 </note>
199
200 @vtable: a function table of type #GThreadFunctions, that provides the
201 entry points to the thread system to be used
202
203
204 <!-- ##### FUNCTION g_thread_supported ##### -->
205 <para>
206 This function returns, whether the thread system is initialized or
207 not.
208 </para>
209
210 <note>
211 <para>
212 This function is actually a macro. Apart from taking the address of it
213 you can however use it as if it was a function.
214 </para>
215 </note>
216
217 @Returns: TRUE, if the thread system is initialized
218
219
220 <!-- ##### USER_FUNCTION GThreadFunc ##### -->
221 <para>
222 Specifies the type of the @func functions passed to
223 g_thread_create() or g_thread_create_full().
224 </para>
225
226 @data: data passed to the thread
227 @Returns: the return value of the thread, which will be returned by
228 g_thread_join()
229
230
231 <!-- ##### ENUM GThreadPriority ##### -->
232 <para>
233 Specifies the priority of a thread. 
234 </para>
235
236 <note>
237 <para>
238 It is not guaranteed, that threads with different priorities really
239 behave accordingly. On some systems (e.g. Linux) only root can increase
240 priorities. On other systems (e.g. Solaris) there doesn't seem to be
241 different scheduling for different priorities. All in all try to avoid
242 being dependent on priorities.
243 </para>
244 </note>
245
246 @G_THREAD_PRIORITY_LOW: a priority lower than normal
247 @G_THREAD_PRIORITY_NORMAL: the default priority
248 @G_THREAD_PRIORITY_HIGH: a priority higher than normal
249 @G_THREAD_PRIORITY_URGENT: the highest priority
250
251 <!-- ##### STRUCT GThread ##### -->
252 <para>
253 The #GThread struct represents a running thread. It has three public
254 read-only members, but the underlying struct is bigger, so you must
255 not copy this struct.
256 </para>
257
258 <note>
259 <para>
260 Resources for a joinable thread are not fully released until
261 g_thread_join() is called for that thread.
262 </para>
263 </note>
264
265 @func: the function executing in that thread
266 @data: the argument to the function
267 @joinable: is this thread joinable?
268 @priority: the priority of the thread
269
270 <!-- ##### FUNCTION g_thread_create ##### -->
271 <para>
272 This function creates a new thread with the priority @priority.
273 </para>
274
275 <para>
276 If @joinable is #TRUE, you can wait for this threads termination
277 calling g_thread_wait(). Otherwise the thread will just disappear, when
278 ready. 
279 </para>
280
281 <para>
282 The new thread executes the function @func with the argument
283 @data. If the thread was created successfully, it is returned.
284 </para>
285
286 <para>
287 @error can be NULL to ignore errors, or non-NULL to report errors. The
288 error is set, if and only if the function returns #NULL.
289 </para>
290
291 @func: a function to execute in the new thread
292 @data: an argument to supply to the new thread
293 @joinable: should this thread be joinable?
294 @error: return location for error.
295 @Returns: the new #GThread on success
296
297
298 <!-- ##### FUNCTION g_thread_create_full ##### -->
299 <para>
300 This function creates a new thread with the priority @priority. The
301 stack gets the size @stack_size or the default value for the current
302 platform, if @stack_size is 0.
303 </para>
304
305 <para>
306 If @joinable is #TRUE, you can wait for this threads termination
307 calling g_thread_wait(). Otherwise the thread will just disappear, when
308 ready. If @bound is #TRUE, this thread will be scheduled in the system
309 scope, otherwise the implementation is free to do scheduling in the
310 process scope. The first variant is more expensive resource-wise, but
311 generally faster. On some systems (e.g. Linux) all threads are bound.
312 </para>
313
314 <para>
315 The new thread executes the function @func with the argument
316 @data. If the thread was created successfully, it is returned.
317 </para>
318
319 <para>
320 @error can be NULL to ignore errors, or non-NULL to report errors. The
321 error is set, if and only if the function returns #NULL.
322 </para>
323
324 <note>
325 <para>
326 It is not guaranteed, that threads with different priorities really
327 behave accordingly. On some systems (e.g. Linux) only root can increase
328 priorities. On other systems (e.g. Solaris) there doesn't seem to be
329 different scheduling for different priorities. All in all try to avoid
330 being dependent on priorities. Use %G_THREAD_PRIORITY_NORMAL here as a
331 default.
332 </para>
333 </note>
334
335 <note>
336 <para>
337 Only use g_thread_create_full(), when you really can't use
338 g_thread_create() instead. g_thread_create() does not take
339 @stack_size, @bound and @priority as arguments, as they should only be
340 used for cases, where it is inevitable. 
341 </para>
342 </note>
343
344 @func: a function to execute in the new thread
345 @data: an argument to supply to the new thread
346 @stack_size: a stack size for the new thread
347 @joinable: should this thread be joinable?
348 @bound: should this thread be bound to a system thread?
349 @priority: a priority for the thread
350 @error: return location for error.
351 @Returns: the new #GThread on success
352
353
354 <!-- ##### FUNCTION g_thread_self ##### -->
355 <para>
356 This functions returns the #GThread corresponding to the calling thread.
357 </para>
358
359 @Returns: the current thread
360
361
362 <!-- ##### FUNCTION g_thread_join ##### -->
363 <para>
364 Waits until @thread finishes, i.e. the function @func, as given
365 to g_thread_create, returns or g_thread_exit() is called by
366 @thread. All resources of @thread including the #GThread struct are
367 released. @thread must have been created with @joinable=#TRUE in
368 g_thread_create(). The value returned by @func or given to
369 g_thread_exit() by @thread is returned by this function.
370 </para>
371
372 @thread: a #GThread to be waited for
373 @Returns: the return value of the thread
374
375
376 <!-- ##### FUNCTION g_thread_set_priority ##### -->
377 <para>
378 Change the priority of @thread to @priority.
379 </para>
380
381 <note>
382 <para>
383 It is not guaranteed, that threads with different priorities really
384 behave accordingly. On some systems (e.g. Linux) only root can increase
385 priorities. On other systems (e.g. Solaris) there doesn't seem to be
386 different scheduling for different priorities. All in all try to avoid
387 being dependent on priorities.
388 </para>
389 </note>
390
391 @thread: a #GThread
392 @priority: a new priority for @thread
393
394
395 <!-- ##### FUNCTION g_thread_yield ##### -->
396 <para>
397 Give way to other threads waiting to be scheduled. 
398 </para>
399
400 <para>
401 This function is often used as a method to make busy wait less
402 evil. But in most cases, you will encounter, there are better methods
403 to do that. So in general you shouldn't use that function.
404 </para>
405
406
407
408 <!-- ##### FUNCTION g_thread_exit ##### -->
409 <para>
410 Exit the current thread. If another thread is waiting for that thread
411 using g_thread_join() and the current thread is joinable, the waiting
412 thread will be woken up and getting @retval as the return value of
413 g_thread_join(). If the current thread is not joinable, @retval is
414 ignored. Calling
415 </para>
416
417 <para>
418 <informalexample>
419 <programlisting>
420 g_thread_join (retval);
421 </programlisting>
422 </informalexample>
423 </para>
424
425 <para>
426 is equivalent to calling 
427 </para>
428
429 <para>
430 <informalexample>
431 <programlisting>
432 return retval;
433 </programlisting>
434 </informalexample>
435 </para>
436
437 <para>
438 in the function @func, as given to g_thread_create().
439 </para>
440
441 <note>
442 <para>
443 Never call g_thread_exit from within a thread of a #GThreadPool, as
444 that will mess up the bookkeeping and lead to funny and unwanted
445 results.
446 </para>
447 </note>
448
449 @retval: the return value of this thread
450
451
452 <!-- ##### STRUCT GMutex ##### -->
453
454 <para>
455 The #GMutex struct is an opaque data structure to represent a mutex
456 (mutual exclusion). It can be used to protect data against shared
457 access. Take for example the following function:
458
459 <example>
460 <title>A function which will not work in a threaded environment</title>
461 <programlisting>
462   int give_me_next_number ()
463   {
464     static int current_number = 0;
465
466     /* now do a very complicated calculation to calculate the new number,
467        this might for example be a random number generator */
468     current_number = calc_next_number (current_number); 
469     return current_number;
470   }
471 </programlisting>
472 </example>
473 </para>
474
475 <para>
476 It is easy to see, that this won't work in a multi-threaded
477 application. There current_number must be protected against shared
478 access. A first naive implementation would be:
479 </para>
480
481 <para>
482 <example>
483 <title>The wrong way to write a thread-safe function</title>
484 <programlisting>
485   int give_me_next_number ()
486   {
487     static int current_number = 0;
488     int ret_val;
489     static GMutex * mutex = NULL;
490
491     if (!mutex)
492       mutex = g_mutex_new ();
493     g_mutex_lock (mutex);
494     ret_val = current_number = calc_next_number (current_number); 
495     g_mutex_unlock (mutex);
496     return ret_val;
497   }
498 </programlisting>
499 </example>
500 </para>
501
502 <para>
503 This looks like it would work, but there is a race condition while
504 constructing the mutex and this code cannot work reliable. So please do
505 not use such constructs in your own programs. One working solution is:
506 </para>
507
508 <para>
509 <example>
510 <title>A correct thread-safe function</title>
511 <programlisting>
512   static GMutex *give_me_next_number_mutex = NULL;
513
514   /* this function must be called before any call to give_me_next_number ()
515      it must be called exactly once. */
516   void init_give_me_next_number () 
517   {
518     g_assert (give_me_next_number_mutex == NULL);
519     give_me_next_number_mutex = g_mutex_new ();
520   }
521
522   int give_me_next_number ()
523   {
524     static int current_number = 0;
525     int ret_val;
526
527     g_mutex_lock (give_me_next_number_mutex);
528     ret_val = current_number = calc_next_number (current_number); 
529     g_mutex_unlock (give_me_next_number_mutex);
530     return ret_val;
531   }
532 </programlisting>
533 </example>
534 </para>
535
536 <para>
537 #GStaticMutex provides a simpler and safer way of doing this.
538 </para>
539
540 <para>
541 If you want to use a mutex, but your code should also work without
542 calling g_thread_init() first, you can not use a #GMutex, as
543 g_mutex_new() requires that. Use a #GStaticMutex instead.
544 </para>
545
546 <para>
547 A #GMutex should only be accessed via the following functions.
548 </para>
549
550 <note>
551 <para>
552 All of the g_mutex_* functions are actually macros. Apart from taking
553 the addresses of them, you can however use them as if they were functions.
554 </para>
555 </note>
556
557
558 <!-- ##### FUNCTION g_mutex_new ##### -->
559
560 <para>
561 Creates a new #GMutex. 
562 </para>
563
564 <note>
565 <para>
566 This function will abort, if g_thread_init() has not been called yet.
567 </para>
568 </note>
569
570 @Returns: a new #GMutex
571
572
573 <!-- ##### FUNCTION g_mutex_lock ##### -->
574
575 <para>
576 Locks @mutex. If @mutex is already locked by another thread, the
577 current thread will block until @mutex is unlocked by the other
578 thread.
579 </para>
580
581 <para>
582 This function can also be used, if g_thread_init() has not yet been
583 called and will do nothing then.
584 </para>
585
586 <note>
587 <para>
588 #GMutex is not recursive, i.e. a thread will deadlock, if it already
589 has locked @mutex while calling g_mutex_lock(). Use
590 #GStaticRecMutex instead, if you need recursive mutexes.
591 </para>
592 </note>
593
594 @mutex: a #GMutex
595
596
597 <!-- ##### FUNCTION g_mutex_trylock ##### -->
598
599 <para>
600 Tries to lock @mutex. If @mutex is already locked by another
601 thread, it immediately returns FALSE. Otherwise it locks @mutex
602 and returns TRUE.
603 </para>
604
605 <para>
606 This function can also be used, if g_thread_init() has not yet been
607 called and will immediately return TRUE then.
608 </para>
609
610 <note>
611 <para>
612 #GMutex is not recursive, i.e. g_mutex_trylock() will return FALSE,
613 if the current thread already has locked @mutex. Use
614 #GStaticRecMutex instead, if you need recursive mutexes.
615 </para>
616 </note>
617
618 @mutex: a #GMutex
619 @Returns: TRUE, if @mutex could be locked
620
621
622 <!-- ##### FUNCTION g_mutex_unlock ##### -->
623
624 <para>
625 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
626 for @mutex, it will be woken and can lock @mutex itself.
627 </para>
628
629 <para>
630 This function can also be used, if g_thread_init() has not yet been
631 called and will do nothing then.
632 </para>
633
634 @mutex: a #GMutex
635
636
637 <!-- ##### FUNCTION g_mutex_free ##### -->
638
639 <para>
640 Destroys @mutex.
641 </para>
642
643 @mutex: a #GMutex
644
645
646 <!-- ##### STRUCT GStaticMutex ##### -->
647
648 <para>
649 A #GStaticMutex works like a #GMutex, but it has one significant
650 advantage. It doesn't need to be created at run-time like a #GMutex,
651 but can be defined at compile-time. Here is a shorter, easier and
652 safer version of our give_me_next_number() example:
653 </para>
654
655 <para>
656 <example>
657 <title>Using GStaticMutex to simplify thread-safe programming</title>
658 <programlisting>
659   int give_me_next_number ()
660   {
661     static int current_number = 0;
662     int ret_val;
663     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
664
665     g_static_mutex_lock (&amp;mutex);
666     ret_val = current_number = calc_next_number (current_number); 
667     g_static_mutex_unlock (&amp;mutex);
668     return ret_val;
669   }
670 </programlisting>
671 </example>
672 </para>
673
674 <para>
675 Sometimes you would like to dynamically create a mutex. If you don't
676 want to require prior calling to g_thread_init(), because your code
677 should also be usable in non-threaded programs, you are not able to
678 use g_mutex_new() and thus #GMutex, as that requires a prior call to
679 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
680 must be initialized with g_static_mutex_init() before using it and
681 freed with with g_static_mutex_free() when not needed anymore to free
682 up any allocated recourses.
683 </para>
684
685 <para>
686 Even though #GStaticMutex is not opaque, it should only be used with
687 the following functions, as it is defined differently on different
688 platforms.
689 </para>
690
691 <para>
692 All of the g_static_mutex_* functions can also be used, if
693 g_thread_init() has not yet been called.
694 </para>
695
696 <note>
697 <para>
698 All of the g_static_mutex_* functions are actually macros. Apart from
699 taking the addresses of them, you can however use them as if they were
700 functions.
701 </para>
702 </note>
703
704
705 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
706
707 <para>
708 A #GStaticMutex must be initialized with this macro, before it can be
709 used. This macro can used be to initialize a variable, but it cannot
710 be assigned to a variable. In that case you have to use
711 g_static_mutex_init().
712 </para>
713
714 <para>
715 <informalexample>
716 <programlisting>
717 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
718 </programlisting>
719 </informalexample>
720 </para>
721
722
723
724 <!-- ##### FUNCTION g_static_mutex_init ##### -->
725 <para>
726 Initializes @mutex. Alternatively you can initialize it with
727 #G_STATIC_MUTEX_INIT.
728 </para>
729
730 @mutex: a #GStaticMutex to be initialized
731
732
733 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
734 <para>
735 Works like g_mutex_lock(), but for a #GStaticMutex.
736 </para>
737
738 @mutex: a #GStaticMutex
739
740
741 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
742
743 <para>
744 Works like g_mutex_trylock(), but for a #GStaticMutex.
745 </para>
746
747 @mutex: a #GStaticMutex
748 @Returns: TRUE, if the #GStaticMutex could be locked
749
750
751 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
752
753 <para>
754 Works like g_mutex_unlock(), but for a #GStaticMutex.
755 </para>
756
757 @mutex: a #GStaticMutex
758
759
760 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
761
762 <para>
763 For some operations (like g_cond_wait()) you must have a #GMutex
764 instead of a #GStaticMutex. This function will return the
765 corresponding #GMutex for @mutex.
766 </para>
767
768 @mutex: a #GStaticMutex
769 @Returns: the #GMutex corresponding to @mutex
770
771
772 <!-- ##### FUNCTION g_static_mutex_free ##### -->
773 <para>
774 Releases all resources allocated to @mutex. 
775 </para>
776
777 <para>
778 You don't have to call this functions for a #GStaticMutex with an
779 unbounded lifetime, i.e. objects declared 'static', but if you have a
780 #GStaticMutex as a member of a structure and the structure is freed,
781 you should also free the #GStaticMutex.
782 </para>
783
784 @mutex: a #GStaticMutex to be freed
785
786
787 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
788
789 <para>
790 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
791 with the advantage that they will expand to nothing in programs
792 compiled against a thread-disabled GLib, saving code and memory
793 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
794 definitions may appear in programs, i.e. in the first block of a
795 function or outside of functions. The @name parameter will be mangled
796 to get the name of the #GStaticMutex. This means, that you can use
797 names of existing variables as the parameter, e.g. the name of the
798 variable you intent to protect with the lock. Look at our
799 give_me_next_number() example using the G_LOCK_* macros:
800 </para>
801
802 <para>
803 <example>
804 <title>Using the G_LOCK_* convenience macros</title>
805 <programlisting>
806 G_LOCK_DEFINE (current_number);
807
808 int give_me_next_number ()
809   {
810     static int current_number = 0;
811     int ret_val;
812
813     G_LOCK (current_number);
814     ret_val = current_number = calc_next_number (current_number); 
815     G_UNLOCK (current_number);
816     return ret_val;
817   }
818 </programlisting>
819 </example>
820 </para>
821
822 @name: the name of the lock
823
824
825 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
826
827 <para>
828 This works like #G_LOCK_DEFINE, but it creates a static object.
829 </para>
830
831 @name: the name of the lock
832
833
834 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
835
836 <para>
837 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
838 </para>
839
840 @name: the name of the lock
841
842
843 <!-- ##### MACRO G_LOCK ##### -->
844
845 <para>
846 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
847 </para>
848
849 @name: the name of the lock
850
851
852 <!-- ##### MACRO G_TRYLOCK ##### -->
853
854 <para>
855 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
856 </para>
857
858 @name: the name of the lock
859 @Returns: TRUE, if the lock could be locked
860
861
862 <!-- ##### MACRO G_UNLOCK ##### -->
863
864 <para>
865 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
866 </para>
867
868 @name: the name of the lock
869
870
871 <!-- ##### STRUCT GStaticRecMutex ##### -->
872 <para>
873 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
874 multiple times by one thread. If you enter it n times, however, you
875 have to unlock it n times again to let other threads lock it. An
876 exception is the function g_static_rec_mutex_unlock_full(), that
877 allows you to unlock a #GStaticRecMutex completely returning the depth,
878 i.e. the number of times this mutex was locked. The depth can later be
879 used to restore the state by calling g_static_rec_mutex_lock_full().
880 </para>
881
882 <para>
883 Even though #GStaticRecMutex is not opaque, it should only be used with
884 the following functions.
885 </para>
886
887 <para>
888 All of the g_static_rec_mutex_* functions can also be used, if
889 g_thread_init() has not been called.
890 </para>
891
892 @mutex: 
893 @depth: 
894 @owner: 
895
896 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
897 <para>
898 A #GStaticRecMutex must be initialized with this macro, before it can
899 be used. This macro can used be to initialize a variable, but it
900 cannot be assigned to a variable. In that case you have to use
901 g_static_rec_mutex_init().
902 </para>
903
904 <para>
905 <informalexample>
906 <programlisting>
907 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
908 </programlisting>
909 </informalexample>
910 </para>
911
912
913
914 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
915 <para>
916 A #GStaticRecMutex must be initialized with this function, before it
917 can be used. Alternatively you can initialize it with
918 #G_STATIC_REC_MUTEX_INIT.
919 </para>
920
921 @mutex: a #GStaticRecMutex to be initialized
922
923
924 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
925 <para>
926 Locks @mutex. If @mutex is already locked by another thread, the
927 current thread will block until @mutex is unlocked by the other
928 thread. If @mutex is already locked by the calling thread, this
929 functions increases the depth of @mutex and returns immediately.
930 </para>
931
932 @mutex: a #GStaticRecMutex to lock
933
934
935 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
936 <para>
937 Tries to lock @mutex. If @mutex is already locked by another thread,
938 it immediately returns #FALSE. Otherwise it locks @mutex and returns
939 #TRUE. If @mutex is already locked by the calling thread, this
940 functions increases the depth of @mutex and immediately  returns #TRUE.
941 </para>
942
943 @mutex: a #GStaticRecMutex to lock
944 @Returns: TRUE, if @mutex could be locked
945
946
947 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
948 <para>
949 Unlocks @mutex. Another threads can, however, only lock @mutex when it
950 has been unlocked as many times, as it had been locked before. If
951 @mutex is completely unlocked and another thread is blocked in a
952 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
953 lock @mutex itself.
954 </para>
955
956 @mutex: a #GStaticRecMutex to unlock
957
958
959 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
960 <para>
961 Works like calling g_static_rec_mutex_lock() for @mutex n times.
962 </para>
963
964 @mutex: a #GStaticRecMutex to lock
965 @depth: number of times this mutex has to be unlocked to be completely unlocked
966
967
968 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
969 <para>
970 Completely unlocks @mutex. If another thread is blocked in a
971 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
972 lock @mutex itself. This function returns the number of times, that
973 @mutex has been locked by the current thread. To restore the state
974 before the call to g_static_rec_mutex_unlock_full() you can call
975 g_static_rec_mutex_lock_full() with the depth returned by this
976 function.
977 </para>
978
979 @mutex: a #GStaticRecMutex to completely unlock
980 @Returns: number of times @mutex has been locked by the current thread
981
982
983 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
984 <para>
985 Releases all resources allocated to a #GStaticRecMutex.
986 </para>
987
988 <para>
989 You don't have to call this functions for a #GStaticRecMutex with an
990 unbounded lifetime, i.e. objects declared 'static', but if you have a
991 #GStaticRecMutex as a member of a structure and the structure is
992 freed, you should also free the #GStaticRecMutex.
993 </para>
994
995 @mutex: a #GStaticRecMutex to be freed
996
997
998 <!-- ##### STRUCT GStaticRWLock ##### -->
999 <para>
1000 The #GStaticRWLock struct represents a read-write lock. A read-write
1001 lock can be used for protecting data, that some portions of code only
1002 read from, while others also write. In such situations it is
1003 desirable, that several readers can read at once, whereas of course
1004 only one writer may write at a time. Take a look at the following
1005 example:
1006
1007 <example>
1008 <title>An array with access functions</title>
1009 <programlisting>
1010   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
1011
1012   GPtrArray *array;
1013
1014   gpointer my_array_get (guint index)
1015   {
1016     gpointer retval = NULL;
1017
1018     if (!array)
1019       return NULL;
1020
1021     g_static_rw_lock_reader_lock (&amp;rwlock);
1022
1023     if (index < array->len)
1024       retval = g_ptr_array_index (array, index);
1025
1026     g_static_rw_lock_reader_unlock (&amp;rwlock);
1027
1028     return retval;
1029   }
1030
1031   void my_array_set (guint index, gpointer data)
1032   {
1033     g_static_rw_lock_writer_lock (&amp;rwlock);
1034
1035     if (!array)
1036       array = g_ptr_array_new ();
1037
1038     if (index >= array->len)
1039       g_ptr_array_set_size (array, index+1);
1040
1041     g_ptr_array_index (array, index) = data; 
1042
1043     g_static_rw_lock_writer_unlock (&amp;rwlock);
1044   }
1045 </programlisting>
1046 </example>
1047 </para>
1048
1049 <para>
1050 This example shows an array, which can be accessed by many readers
1051 (the my_array_get function) simultaneously, whereas the writers (the
1052 my_array_set function) only will be allowed once a time and only if no
1053 readers currently access the array. This is because of the potentially
1054 dangerous resizing of the array. Using that functions is fully
1055 multi-thread safe now. 
1056 </para>
1057
1058 <para>
1059 Most of the time the writers should have precedence of readers. That
1060 means for this implementation, that as soon as a writer wants to lock
1061 the data, no other reader is allowed to lock the data, whereas of
1062 course the readers, that already have locked the data are allowed to
1063 finish their operation. As soon as the last reader unlocks the data,
1064 the writer will lock it.
1065 </para>
1066
1067 <para>
1068 Even though #GStaticRWLock is not opaque, it should only be used with
1069 the following functions.
1070 </para>
1071
1072 <para>
1073 All of the g_static_rw_lock_* functions can also be used, if
1074 g_thread_init() has not been called.
1075 </para>
1076
1077 <note>
1078 <para>
1079 A read-write lock has a higher overhead as a mutex. For example both
1080 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1081 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1082 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1083 #GStaticMutex. So only data structures, that are accessed by multiple
1084 readers, which keep the lock for a considerable time justify a
1085 #GStaticRWLock. The above example most probably would fare better with
1086 a #GStaticMutex.
1087 </para>
1088 </note>
1089
1090 @mutex: 
1091 @read_cond: 
1092 @write_cond: 
1093 @read_counter: 
1094 @write: 
1095 @want_to_write: 
1096
1097 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1098 <para>
1099 A #GStaticRWLock must be initialized with this macro, before it can
1100 be used. This macro can used be to initialize a variable, but it
1101 cannot be assigned to a variable. In that case you have to use
1102 g_static_rw_lock_init().
1103 </para>
1104
1105 <para>
1106 <informalexample>
1107 <programlisting>
1108 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1109 </programlisting>
1110 </informalexample>
1111 </para>
1112
1113
1114
1115 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1116 <para>
1117 A #GStaticRWLock must be initialized with this function, before it can
1118 be used. Alternatively you can initialize it with
1119 #G_STATIC_RW_LOCK_INIT.
1120 </para>
1121
1122 @lock: a #GStaticRWLock to be initialized
1123
1124
1125 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1126 <para>
1127 Locks @lock for reading. There may be unlimited concurrent locks for
1128 reading of a #GStaticRWLock at the same time.  If @lock is already
1129 locked for writing by another thread or if another thread is already
1130 waiting to lock @lock for writing, this function will block until
1131 @lock is unlocked by the other writing thread and no other writing
1132 threads want to lock @lock. This lock has to be unlocked by
1133 g_static_rw_lock_reader_unlock().
1134 </para>
1135
1136 <para>
1137 #GStaticRWLock in general is not recursive, but as there may be
1138 unlimited concurrent locks for reading, it effectively is for
1139 recursive for reading, but for reading only. Locking for writing after
1140 locking for reading will deadlock, the same holds true for the
1141 opposite order.
1142 </para>
1143
1144 @lock: a #GStaticRWLock to lock for reading
1145
1146
1147 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1148 <para>
1149 Tries to lock @lock for reading. If @lock is already locked for
1150 writing by another thread or if another thread is already waiting to
1151 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1152 locks @lock for reading and returns TRUE. This lock has to be unlocked
1153 by g_static_rw_lock_reader_unlock().
1154 </para>
1155
1156 @lock: a #GStaticRWLock to lock for reading
1157 @Returns: TRUE, if @lock could be locked for reading
1158
1159
1160 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1161 <para>
1162 Unlocks @lock. If a thread waits to lock @lock for writing and all
1163 locks for reading have been unlocked, the waiting thread is woken up
1164 and can lock @lock for writing.
1165 </para>
1166
1167 @lock: a #GStaticRWLock to unlock after reading
1168
1169
1170 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1171 <para>
1172 Locks @lock for writing. If @lock is already locked for writing or
1173 reading by other threads, this function will block until @lock is
1174 completely unlocked and then lock @lock for writing. While this
1175 functions waits to lock @lock, no other thread can lock @lock for
1176 reading. When @lock is locked for writing, no other thread can lock
1177 @lock (neither for reading nor writing). This lock has to be unlocked
1178 by g_static_rw_lock_writer_unlock().
1179 </para>
1180
1181 @lock: a #GStaticRWLock to lock for writing
1182
1183
1184 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1185 <para>
1186 Tries to lock @lock for writing. If @lock is already locked (for
1187 either reading or writing) by another thread, it immediately returns
1188 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1189 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1190 </para>
1191
1192 @lock: a #GStaticRWLock to lock for writing
1193 @Returns: TRUE, if @lock could be locked for writing
1194
1195
1196 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1197 <para>
1198 Unlocks @lock. If a thread waits to lock @lock for writing and all
1199 locks for reading have been unlocked, the waiting thread is woken up
1200 and can lock @lock for writing. If no thread waits to lock @lock for
1201 writing and threads wait to lock @lock for reading, the waiting
1202 threads are woken up and can lock @lock for reading.
1203 </para>
1204
1205 @lock: a #GStaticRWLock to unlock after writing
1206
1207
1208 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1209 <para>
1210 Releases all resources allocated to @lock. 
1211 </para>
1212
1213 <para>
1214 You don't have to call this functions for a #GStaticRWLock with an
1215 unbounded lifetime, i.e. objects declared 'static', but if you have a
1216 #GStaticRWLock as a member of a structure and the structure is freed,
1217 you should also free the #GStaticRWLock.
1218 </para>
1219
1220 @lock: a #GStaticRWLock to be freed
1221
1222
1223 <!-- ##### STRUCT GCond ##### -->
1224
1225 <para>
1226 The #GCond struct is an opaque data structure to represent a
1227 condition. A #GCond is an object, that threads can block on, if they
1228 find a certain condition to be false. If other threads change the
1229 state of this condition they can signal the #GCond, such that the
1230 waiting thread is woken up. 
1231 </para>
1232
1233 <para>
1234 <example>
1235 <title>Using GCond to block a thread until a condition is satisfied</title>
1236 <programlisting>
1237 GCond* data_cond = NULL;   /* Must be initialized somewhere */
1238 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1239 gpointer current_data = NULL;
1240
1241 void push_data (gpointer data)
1242 {
1243   g_mutex_lock (data_mutex);
1244   current_data = data;
1245   g_cond_signal (data_cond);
1246   g_mutex_unlock (data_mutex);
1247 }
1248
1249 gpointer pop_data ()
1250 {
1251   gpointer data;
1252
1253   g_mutex_lock (data_mutex);
1254   while (!current_data)
1255       g_cond_wait (data_cond, data_mutex);
1256   data = current_data;
1257   current_data = NULL;
1258   g_mutex_unlock (data_mutex);
1259   return data;
1260 }
1261 </programlisting>
1262 </example>
1263 </para>
1264
1265 <para>
1266 Whenever a thread calls pop_data() now, it will wait until
1267 current_data is non-NULL, i.e. until some other thread has called
1268 push_data().
1269 </para>
1270
1271 <note>
1272 <para>
1273 It is important to use the g_cond_wait() and g_cond_timed_wait()
1274 functions only inside a loop, which checks for the condition to be
1275 true as it is not guaranteed that the waiting thread will find it
1276 fulfilled, even if the signaling thread left the condition
1277 in that state. This is because another thread can have altered the
1278 condition, before the waiting thread got the chance to be woken up,
1279 even if the condition itself is protected by a #GMutex, like above.
1280 </para>
1281 </note>
1282
1283 <para>
1284 A #GCond should only be accessed via the following functions.
1285 </para>
1286
1287 <note>
1288 <para>
1289 All of the g_cond_* functions are actually macros. Apart from taking
1290 the addresses of them, you can however use them as if they were functions.
1291 </para>
1292 </note>
1293
1294
1295 <!-- ##### FUNCTION g_cond_new ##### -->
1296
1297 <para>
1298 Creates a new #GCond. This function will abort, if g_thread_init()
1299 has not been called yet.
1300 </para>
1301
1302 @Returns: a new #GCond
1303
1304
1305 <!-- ##### FUNCTION g_cond_signal ##### -->
1306 <para>
1307 If threads are waiting for @cond, exactly one of them is woken up. It
1308 is good practice to hold the same lock as the waiting thread, while
1309 calling this function, though not required.
1310 </para>
1311
1312 <para>
1313 This function can also be used, if g_thread_init() has
1314 not yet been called and will do nothing then.
1315 </para>
1316
1317 @cond: a #GCond
1318
1319
1320 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1321
1322 <para>
1323 If threads are waiting for @cond, all of them are woken up. It is good
1324 practice to lock the same mutex as the waiting threads, while calling
1325 this function, though not required.
1326 </para>
1327
1328 <para>
1329 This function can also be used, if g_thread_init() has
1330 not yet been called and will do nothing then.
1331 </para>
1332
1333 @cond: a #GCond
1334
1335
1336 <!-- ##### FUNCTION g_cond_wait ##### -->
1337
1338 <para>
1339 Waits until this thread is woken up on @cond. The @mutex is unlocked
1340 before falling asleep and locked again before resuming.
1341 </para>
1342
1343 <para>
1344 This function can also be used, if g_thread_init() has not yet been
1345 called and will immediately return then.
1346 </para>
1347
1348 @cond: a #GCond
1349 @mutex: a #GMutex, that is currently locked
1350
1351
1352 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1353
1354 <para>
1355 Waits until this thread is woken up on @cond, but not longer than
1356 until the time, that is specified by @abs_time. The @mutex is
1357 unlocked before falling asleep and locked again before resuming.
1358 </para>
1359
1360 <para>
1361 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1362 </para>
1363
1364 <para>
1365 This function can also be used, if g_thread_init() has not yet been
1366 called and will immediately return TRUE then.
1367 </para>
1368
1369 @cond: a #GCond
1370 @mutex: a #GMutex, that is currently locked
1371 @abs_time: a #GTimeVal, determining the final time
1372 @Returns: TRUE, if the thread is woken up in time
1373
1374
1375 <!-- ##### FUNCTION g_cond_free ##### -->
1376
1377 <para>
1378 Destroys the #GCond.
1379 </para>
1380
1381 @cond: a #GCond
1382
1383
1384 <!-- ##### STRUCT GPrivate ##### -->
1385 <para>
1386 The #GPrivate struct is an opaque data structure to represent a thread
1387 private data key. Threads can thereby obtain and set a pointer, which
1388 is private to the current thread. Take our give_me_next_number()
1389 example from above.  Now we don't want current_number to be shared
1390 between the threads, but to be private to each thread. This can be
1391 done as follows:
1392
1393 <example>
1394 <title>Using GPrivate for per-thread data</title>
1395 <programlisting>
1396   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1397                                        /* with g_private_new (g_free); */
1398
1399   int give_me_next_number ()
1400   {
1401     int *current_number = g_private_get (current_number_key);
1402
1403     if (!current_number)
1404     {
1405       current_number = g_new (int,1);
1406       *current_number = 0;
1407       g_private_set (current_number_key, current_number);
1408     }
1409     *current_number = calc_next_number (*current_number); 
1410     return *current_number;
1411   }
1412 </programlisting>
1413 </example>
1414 </para>
1415
1416 <para>
1417 Here the pointer belonging to the key current_number_key is read. If
1418 it is NULL, it has not been set yet. Then get memory for an integer
1419 value, assign this memory to the pointer and write the pointer
1420 back. Now we have an integer value, that is private to the current
1421 thread.
1422 </para>
1423
1424 <para>
1425 The #GPrivate struct should only be accessed via the following functions.
1426 </para>
1427
1428 <note>
1429 <para>
1430 All of the g_private_* functions are actually macros. Apart from taking
1431 the addresses of them, you can however use them as if they were functions.
1432 </para>
1433 </note>
1434
1435
1436 <!-- ##### FUNCTION g_private_new ##### -->
1437
1438 <para>
1439 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1440 to a destructor function. Whenever a thread ends and the corresponding
1441 pointer keyed to this instance of #GPrivate is non-NULL, the
1442 destructor is called with this pointer as the argument.
1443 </para>
1444
1445 <note>
1446 <para>
1447 @destructor is working quite differently from @notify in
1448 g_static_private_set().
1449 </para>
1450 </note>
1451
1452 <note>
1453 <para>
1454 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1455 shortage or use #GStaticPrivate.
1456 </para>
1457 </note>
1458
1459 <note>
1460 <para>
1461 This function will abort, if g_thread_init() has not been called yet.
1462 </para>
1463 </note>
1464
1465 @destructor: a function to handle the data keyed to #GPrivate, when a
1466 thread ends
1467 @Returns: a new #GPrivate
1468
1469
1470 <!-- ##### FUNCTION g_private_get ##### -->
1471
1472 <para>
1473 Returns the pointer keyed to @private_key for the current thread. This
1474 pointer is NULL, when g_private_set() hasn't been called for the
1475 current @private_key and thread yet.
1476 </para>
1477
1478 <para>
1479 This function can also be used, if g_thread_init() has not yet been
1480 called and will return the value of @private_key casted to #gpointer then.
1481 </para>
1482
1483 @private_key: a #GPrivate
1484 @Returns: the corresponding pointer
1485
1486
1487 <!-- ##### FUNCTION g_private_set ##### -->
1488
1489 <para>
1490 Sets the pointer keyed to @private_key for the current thread.
1491 </para>
1492
1493 <para>
1494 This function can also be used, if g_thread_init() has not yet been
1495 called and will set @private_key to @data casted to #GPrivate* then.
1496 </para>
1497
1498 @private_key: a #GPrivate
1499 @data: the new pointer
1500
1501
1502 <!-- ##### STRUCT GStaticPrivate ##### -->
1503
1504 <para>
1505 A #GStaticPrivate works almost like a #GPrivate, but it has one
1506 significant advantage. It doesn't need to be created at run-time like
1507 a #GPrivate, but can be defined at compile-time. This is similar to
1508 the difference between #GMutex and #GStaticMutex. Now look at our
1509 give_me_next_number() example with #GStaticPrivate:
1510 </para>
1511
1512 <para>
1513 <example>
1514 <title>Using GStaticPrivate for per-thread data</title>
1515 <programlisting>
1516   int give_me_next_number ()
1517   {
1518     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1519     int *current_number = g_static_private_get (&amp;current_number_key);
1520
1521     if (!current_number)
1522     {
1523       current_number = g_new (int,1);
1524       *current_number = 0;
1525       g_static_private_set (&amp;current_number_key, current_number, g_free);
1526     }
1527     *current_number = calc_next_number (*current_number); 
1528     return *current_number;
1529   }
1530 </programlisting>
1531 </example>
1532 </para>
1533
1534 @index: 
1535
1536 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1537 <para>
1538 Every #GStaticPrivate must be initialized with this macro, before it can
1539 be used.
1540 </para>
1541
1542 <para>
1543 <informalexample>
1544 <programlisting>
1545 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1546 </programlisting>
1547 </informalexample>
1548 </para>
1549
1550
1551
1552 <!-- ##### FUNCTION g_static_private_init ##### -->
1553 <para>
1554 Initializes @private_key. Alternatively you can initialize it with
1555 #G_STATIC_PRIVATE_INIT.
1556 </para>
1557
1558 @private_key: a #GStaticPrivate to be initialized
1559
1560
1561 <!-- ##### FUNCTION g_static_private_get ##### -->
1562 <para>
1563 Works like g_private_get() only for a #GStaticPrivate.
1564 </para>
1565
1566 <para>
1567 This function also works, if g_thread_init() has not yet been called.
1568 </para>
1569
1570 @private_key: a #GStaticPrivate
1571 @Returns: the corresponding pointer
1572
1573
1574 <!-- ##### FUNCTION g_static_private_set ##### -->
1575 <para>
1576 Sets the pointer keyed to @private_key for the current thread and the
1577 function @notify to be called with that pointer (NULL or non-NULL),
1578 whenever the pointer is set again or whenever the current thread ends.
1579 </para>
1580
1581 <para>
1582 This function also works, if g_thread_init() has not yet been
1583 called. If g_thread_init() is called later, the @data keyed to
1584 @private_key will be inherited only by the main thread, i.e. the one that
1585 called g_thread_init().
1586 </para>
1587
1588 <note>
1589 <para>
1590 @notify is working quite differently from @destructor in
1591 g_private_new().
1592 </para>
1593 </note>
1594
1595 @private_key: a #GStaticPrivate
1596 @data: the new pointer
1597 @notify: a function to be called with the pointer, whenever the
1598 current thread ends or sets this pointer again
1599
1600
1601 <!-- ##### FUNCTION g_static_private_free ##### -->
1602 <para>
1603 Releases all resources allocated to @private_key. 
1604 </para>
1605
1606 <para>
1607 You don't have to call this functions for a #GStaticPrivate with an
1608 unbounded lifetime, i.e. objects declared 'static', but if you have a
1609 #GStaticPrivate as a member of a structure and the structure is freed,
1610 you should also free the #GStaticPrivate.
1611 </para>
1612
1613 @private_key: a #GStaticPrivate to be freed
1614
1615