1 <!-- ##### SECTION Title ##### -->
5 <!-- ##### SECTION Short_Description ##### -->
7 thread abstraction; including mutexes, conditions and thread private data.
9 <!-- ##### SECTION Long_Description ##### -->
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.
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,
34 Currently there is only as much thread support included in GLib as is
35 necessary to make GLib itself multithread safe. Future versions of
36 GLib might contain functions to actually create threads and the
37 like. For now the most portable way to create threads is to require
38 the macro #G_THREADS_IMPL_POSIX to be defined and use POSIX threads
39 then. This will work on almost all platforms (except most notably
43 <!-- ##### SECTION See_Also ##### -->
48 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
51 This macro is defined, if GLib was compiled with thread support. This
52 does not necessarily mean, that there is a thread implementation
53 available, but the infrastructure is in place and once you provide a
54 thread implementation to g_thread_init(), GLib will be multithread
55 safe. It isn't and can't be, if #G_THREADS_ENABLED is not defined.
60 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
63 This macro is defined, if POSIX style threads are used.
68 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
71 This macro is defined, if the SOLARIS thread system is used.
76 <!-- ##### MACRO G_THREADS_IMPL_NSPR ##### -->
79 This macro is defined, if the NSPR thread implementation is used.
80 NSPR is the cross platform library of mozilla.
85 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
88 This macro is defined, if no thread implementation is used. You can
89 however provide one to g_thread_init() to make GLib multithread safe.
94 <!-- ##### STRUCT GThreadFunctions ##### -->
97 This function table is used by g_thread_init() to initialize the
98 thread system. The functions in that table are directly used by their
99 g_* prepended counterparts, that are described here, e.g. if you call
100 g_mutex_new() then mutex_new() from the table provided to
101 g_thread_init() will be called.
106 This struct should only be used, if you know, what you are doing.
125 <!-- ##### FUNCTION g_thread_init ##### -->
128 Before you use a thread related function in GLib, you should
129 initialize the thread system. This is done by calling
130 g_thread_init(). Most of the time you will only have to call
136 You should only call g_thread_init() with a non-NULL parameter, if you
137 really know, what you are doing.
143 g_thread_init() must not be called directly or indirectly as a
149 g_thread_init() might only be called once. On the second call
150 it will abort with an error. If you want to make sure, that the thread
151 system is initialized, you can do that too:
157 if (!g_thread_supported ()) g_thread_init (NULL);
163 After that line either the thread system is initialized or the program
164 will abort, if no thread system is available in GLib, i.e. either
165 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
169 If no thread system is available and @vtable is NULL or if not all
170 elements of @vtable are non-NULL, then g_thread_init() will abort.
175 To use g_thread_init() in your program, you have to link with the
176 libraries, that the command "glib-config --libs gthread" outputs. This
177 is not the case for all the other thread related functions of
178 GLib. Those can be used without having to link with the thread
183 @vtable: a function table of type #GThreadFunctions, that provides the
184 entry points to the thread system to be used.
187 <!-- ##### MACRO g_thread_supported ##### -->
189 This function returns, whether the thread system is initialized or
195 This function is actually a macro. Apart from taking the address of it
196 you can however use it as if it was a function.
200 @Returns: TRUE, if the thread system is initialized.
203 <!-- ##### STRUCT GMutex ##### -->
206 The #GMutex struct is an opaque data structure to represent a mutex
207 (mutual exclusion). It can be used to protect data against shared
208 access. Take for example the following function:
211 <title>A function which will not work in a threaded environment</title>
213 int give_me_next_number ()
215 static int current_number = 0;
217 /* now do a very complicated calculation to calculate the new number,
218 this might for example be a random number generator */
219 current_number = calc_next_number (current_number);
220 return current_number;
227 It is easy to see, that this won't work in a multithreaded
228 application. There current_number must be protected against shared
229 access. A first naive implementation would be:
234 <title>The wrong way to write a thread-safe function</title>
236 int give_me_next_number ()
238 static int current_number = 0;
240 static GMutex * mutex = NULL;
243 mutex = g_mutex_new ();
244 g_mutex_lock (mutex);
245 ret_val = current_number = calc_next_number (current_number);
246 g_mutex_unlock (mutex);
254 This looks like it would work, but there is a race condition while
255 constructing the mutex and this code can't work reliable. So please do
256 not use such constructs in your own programs. One working solution is:
261 <title>A correct thread-safe function</title>
263 static GMutex *give_me_next_number_mutex = NULL;
265 /* this function must be called before any call to give_me_next_number ()
266 it must be called exactly once. */
267 void init_give_me_next_number ()
269 g_assert (give_me_next_number_mutex == NULL);
270 give_me_next_number_mutex = g_mutex_new ();
273 int give_me_next_number ()
275 static int current_number = 0;
278 g_mutex_lock (give_me_next_number_mutex);
279 ret_val = current_number = calc_next_number (current_number);
280 g_mutex_unlock (give_me_next_number_mutex);
288 #GStaticMutex provides a simpler and safer way of doing this.
292 A #GMutex should only be accessed via the following functions.
297 All of the g_mutex_* functions are actually macros. Apart from taking
298 the addresses of them, you can however use them as if they were functions.
303 <!-- ##### MACRO g_mutex_new ##### -->
306 Creates a new #GMutex.
311 This function will abort, if g_thread_init() has not been called yet.
315 @Returns: a new #GMutex.
318 <!-- ##### MACRO g_mutex_lock ##### -->
321 Locks the #GMutex. If the #GMutex is already locked by another thread,
322 the current thread will block until the #GMutex is unlocked by the
327 This function can also be used, if g_thread_init() has not yet been
328 called and will do nothing then.
333 #GMutex is not guaranteed to be recursive, i.e. a thread might block,
334 if it already has locked the #GMutex. It will deadlock then, of
342 <!-- ##### MACRO g_mutex_trylock ##### -->
345 Tries to lock the #GMutex. If the #GMutex is already locked by another
346 thread, it immediately returns FALSE. Otherwise it locks the #GMutex
351 This function can also be used, if g_thread_init() has not yet been
352 called and will immediately return TRUE then.
356 @Returns: TRUE, if the #GMutex could be locked.
359 <!-- ##### MACRO g_mutex_unlock ##### -->
362 Unlocks the #GMutex. If another thread is blocked in a g_mutex_lock()
363 call, it will be woken and can lock the #GMutex itself. This function
364 can also be used, if g_thread_init() has not yet been called and will
371 <!-- ##### MACRO g_mutex_free ##### -->
374 Destroys the #GMutex.
380 <!-- ##### STRUCT GStaticMutex ##### -->
383 A #GStaticMutex works like a #GMutex, but it has one significant
384 advantage. It doesn't need to be created at run-time like a #GMutex,
385 but can be defined at compile-time. Here is a shorter, easier and
386 safer version of our give_me_next_number() example:
391 <title>Using GStaticMutex to simplify thread-safe programming</title>
393 int give_me_next_number ()
395 static int current_number = 0;
397 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
399 g_static_mutex_lock (&mutex);
400 ret_val = current_number = calc_next_number (current_number);
401 g_static_mutex_unlock (&mutex);
409 Even though #GStaticMutex is not opaque, it should only be used with
410 the following functions, as it is defined differently on different
414 <para>All of the g_static_mutex_* functions can also be used, if
415 g_thread_init() has not yet.
420 All of the g_static_mutex_* functions are actually macros. Apart from
421 taking the addresses of them, you can however use them as if they were
428 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
431 Every #GStaticMutex must be initialized with this macro, before it can
437 <title>Initializing a GStaticMutext</title>
439 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
446 <!-- ##### MACRO g_static_mutex_lock ##### -->
448 works like g_mutex_lock(), but for a #GStaticMutex.
451 @mutex: a #GStaticMutex.
454 <!-- ##### MACRO g_static_mutex_trylock ##### -->
457 works like g_mutex_trylock(), but for a #GStaticMutex.
460 @mutex: a #GStaticMutex.
461 @Returns: TRUE, if the #GStaticMutex could be locked.
464 <!-- ##### MACRO g_static_mutex_unlock ##### -->
467 works like g_mutex_unlock(), but for a #GStaticMutex.
470 @mutex: a #GStaticMutex.
473 <!-- ##### MACRO g_static_mutex_get_mutex ##### -->
476 For some operations (like g_cond_wait()) you must have a #GMutex
477 instead of a #GStaticMutex. This function will return the
478 corresponding #GMutex for every #GStaticMutex.
481 @mutex: a #GStaticMutex.
482 @Returns: the corresponding #GMutex.
485 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
488 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
489 with the advantage that they will expand to nothing in programs
490 compiled against a thread-disabled GLib, saving code and memory
491 there. #G_LOCK_DEFINE defines a lock. It can occur, where variable
492 definitions may occur in programs, i.e. in the first block of a
493 function or outside of functions. The @name parameter will be mangled
494 to get the name of the #GStaticMutex. This means, that you can use
495 names of existing variables as the parameter, e.g. the name of the
496 variable you intent to protect with the lock. Look at our
497 give_me_next_number() example using the G_LOCK_* macros:
502 <title>Using the G_LOCK_* convenience macros</title>
504 int give_me_next_number ()
506 static int current_number = 0;
508 G_LOCK_DEFINE_STATIC (current_number);
510 G_LOCK (current_number);
511 ret_val = current_number = calc_next_number (current_number);
512 G_UNLOCK (current_number);
519 @name: the name of the lock.
522 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
525 This works like #G_LOCK_DEFINE, but it creates a static object.
528 @name: the name of the lock.
531 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
534 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
537 @name: the name of the lock.
540 <!-- ##### MACRO G_LOCK ##### -->
543 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
546 @name: the name of the lock.
549 <!-- ##### MACRO G_TRYLOCK ##### -->
552 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
555 @name: the name of the lock.
556 @Returns: TRUE, if the lock could be locked.
559 <!-- ##### MACRO G_UNLOCK ##### -->
562 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
565 @name: the name of the lock.
568 <!-- ##### STRUCT GCond ##### -->
571 The #GCond struct is an opaque data structure to represent a
572 condition. A #GCond is an object, that threads can block on, if they
573 find a certain condition to be false. If other threads change the
574 state of this condition they can signal the #GCond, such that the
575 waiting thread is woken up.
580 <title>Using GCond to block a thread until a condition is satisfied</title>
582 GCond* data_cond = NULL; /* Must be initialized somewhere */
583 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
584 gpointer current_data = NULL;
586 void push_data (gpointer data)
588 g_mutex_lock (data_mutex);
590 g_cond_signal (data_cond);
591 g_mutex_unlock (data_mutex);
598 g_mutex_lock (data_mutex);
599 while (!current_data)
600 g_cond_wait (data_cond, data_mutex);
603 g_mutex_unlock (data_mutex);
611 Whenever a thread calls pop_data() now, it will wait until
612 current_data is non-NULL, i.e. until some other thread has called
618 It is important to use the g_cond_wait() and g_cond_timed_wait()
619 functions only inside a loop, which checks for the condition to be
620 true as it is not guaranteed that the waiting thread will find it
621 fulfilled, even if the signaling thread left the condition
622 in that state. This is because another thread can have altered the
623 condition, before the waiting thread got the chance to be woken up,
624 even if the condition itself is protected by a #GMutex, like above.
629 A #GCond should only be accessed via the following functions.
634 All of the g_cond_* functions are actually macros. Apart from taking
635 the addresses of them, you can however use them as if they were functions.
640 <!-- ##### MACRO g_cond_new ##### -->
643 Creates a new #GCond. This function will abort, if g_thread_init()
644 has not been called yet.
647 @Returns: a new #GCond.
650 <!-- ##### MACRO g_cond_signal ##### -->
652 If threads are waiting for @cond, exactly one of them is woken up. It
653 is good practice to hold the same lock as the waiting thread, while
654 calling this function, though not required.
658 This function can also be used, if g_thread_init() has
659 not yet been called and will do nothing then.
665 <!-- ##### MACRO g_cond_broadcast ##### -->
668 If threads are waiting for @cond, all of them are woken up. It is good
669 practice to lock the same mutex as the waiting threads, while calling
670 this function, though not required.
674 This function can also be used, if g_thread_init() has
675 not yet been called and will do nothing then.
681 <!-- ##### MACRO g_cond_wait ##### -->
684 Waits until this thread is woken up on the #GCond. The #GMutex is
685 unlocked before falling asleep and locked again before resuming.
689 This function can also be used, if g_thread_init() has not yet been
690 called and will immediately return then.
694 @mutex: the #GMutex, that is currently locked.
697 <!-- ##### MACRO g_cond_timed_wait ##### -->
700 Waits until this thread is woken up on the #GCond, but not longer than
701 until the time, that is specified by @abs_time. The #GMutex is
702 unlocked before falling asleep and locked again before resuming.
706 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
710 This function can also be used, if g_thread_init() has not yet been
711 called and will immediately return TRUE then.
715 @mutex: the #GMutex, that is currently locked.
716 @abs_time: a #GTimeVal, determining the final time.
717 @Returns: TRUE, if the thread is woken up in time.
720 <!-- ##### MACRO g_cond_free ##### -->
729 <!-- ##### STRUCT GPrivate ##### -->
731 The #GPrivate struct is an opaque data structure to represent a thread
732 private data key. Threads can thereby obtain and set a pointer, which
733 is private to the current thread. Take our give_me_next_number()
734 example from above. Now we don't want current_number to be shared
735 between the threads, but to be private to each thread. This can be
739 <title>Using GPrivate for per-thread data</title>
741 GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
742 /* with g_private_new (g_free); */
744 int give_me_next_number ()
746 int *current_number = g_private_get (current_number_key);
750 current_number = g_new (int,1);
752 g_private_set (current_number_key, current_number);
754 *current_number = calc_next_number (*current_number);
755 return *current_number;
762 Here the pointer belonging to the key current_number_key is read. If
763 it is NULL, it has not been set yet. Then get memory for an integer
764 value, assign this memory to the pointer and write the pointer
765 back. Now we have an integer value, that is private to the current
770 The #GPrivate struct should only be accessed via the following functions.
775 All of the g_private_* functions are actually macros. Apart from taking
776 the addresses of them, you can however use them as if they were functions.
781 <!-- ##### MACRO g_private_new ##### -->
784 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
785 to a destructor function. Whenever a thread ends and the corresponding
786 pointer keyed to this instance of #GPrivate is non-NULL, the
787 destructor is called with this pointer as the argument.
792 The @destructor is working quite differently from @notify in
793 g_static_private_set().
799 A #GPrivate can not be destroyed. Reuse it instead, if you can to
806 This function will abort, if g_thread_init() has not been called yet.
810 @destructor: a function to handle the data keyed to #GPrivate, when a
814 <!-- ##### MACRO g_private_get ##### -->
817 Returns the pointer keyed to @private_key for the current thread. This
818 pointer is NULL, when g_private_set() hasn't been called for the
819 current @private_key and thread yet.
823 This function can also be used, if g_thread_init() has not yet been
824 called and will return the value of @private_key casted to #gpointer then.
827 @private_key: a #GPrivate.
828 @Returns: the corresponding pointer.
831 <!-- ##### MACRO g_private_set ##### -->
834 Sets the pointer keyed to @private_key for the current thread.
838 This function can also be used, if g_thread_init() has not yet been
839 called and will set @private_key to @data casted to #GPrivate* then.
842 @private_key: a #GPrivate.
844 <!-- # Unused Parameters # -->
845 @data: the new pointer.
848 <!-- ##### STRUCT GStaticPrivate ##### -->
851 A #GStaticPrivate works almost like a #GPrivate, but it has one
852 significant advantage. It doesn't need to be created at run-time like
853 a #GPrivate, but can be defined at compile-time. This is similar to
854 the difference between #GMutex and #GStaticMutex. Now look at our
855 give_me_next_number() example with #GStaticPrivate:
860 <title>Using GStaticPrivate for per-thread data</title>
862 int give_me_next_number ()
864 static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
865 int *current_number = g_static_private_get (&current_number_key);
869 current_number = g_new (int,1);
871 g_static_private_set (&current_number_key, current_number, g_free);
873 *current_number = calc_next_number (*current_number);
874 return *current_number;
882 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
884 Every #GStaticPrivate must be initialized with this macro, before it can
891 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
898 <!-- ##### FUNCTION g_static_private_get ##### -->
900 Works like g_private_get() only for a #GStaticPrivate.
904 This function also works, if g_thread_init() has not yet been called.
907 @private_key: a #GStaticPrivate.
908 @Returns: the corresponding pointer.
911 <!-- ##### FUNCTION g_static_private_set ##### -->
913 Sets the pointer keyed to @private_key for the current thread and the
914 function @notify to be called with that pointer (NULL or non-NULL),
915 whenever the pointer is set again or whenever the current thread ends.
919 This function also works, if g_thread_init() has not yet been
920 called. If g_thread_init() is called later, the @data keyed to
921 @private_key will be inherited only by the main thread, i.e. the one that
922 called g_thread_init().
927 The @notify is working quite differently from @destructor in
932 @private_key: a #GStaticPrivate.
933 @data: the new pointer.
934 @notify: a function to be called with the pointer, whenever the
935 current thread ends or sets this pointer again.