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,
33 <!-- ##### SECTION See_Also ##### -->
38 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
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.
50 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
53 This macro is defined, if POSIX style threads are used.
58 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
61 This macro is defined, if the SOLARIS thread system is used.
66 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
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.
75 <!-- ##### MACRO G_THREAD_ERROR ##### -->
82 <!-- ##### ENUM GThreadError ##### -->
87 @G_THREAD_ERROR_AGAIN:
89 <!-- ##### STRUCT GThreadFunctions ##### -->
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.
101 This struct should only be used, if you know, what you are doing.
123 @thread_set_priority:
126 <!-- ##### FUNCTION g_thread_init ##### -->
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
137 You should only call g_thread_init() with a non-NULL parameter, if you
138 really know, what you are doing.
144 g_thread_init() must not be called directly or indirectly as a
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:
158 if (!g_thread_supported ()) g_thread_init (NULL);
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.
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.
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
184 @vtable: a function table of type #GThreadFunctions, that provides the
185 entry points to the thread system to be used.
188 <!-- ##### FUNCTION g_thread_supported ##### -->
190 This function returns, whether the thread system is initialized or
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.
201 @Returns: TRUE, if the thread system is initialized.
204 <!-- ##### USER_FUNCTION GThreadFunc ##### -->
212 <!-- ##### ENUM GThreadPriority ##### -->
217 @G_THREAD_PRIORITY_LOW:
218 @G_THREAD_PRIORITY_NORMAL:
219 @G_THREAD_PRIORITY_HIGH:
220 @G_THREAD_PRIORITY_URGENT:
222 <!-- ##### STRUCT GThread ##### -->
231 <!-- ##### FUNCTION g_thread_create ##### -->
246 <!-- ##### FUNCTION g_thread_self ##### -->
254 <!-- ##### FUNCTION g_thread_join ##### -->
262 <!-- ##### FUNCTION g_thread_set_priority ##### -->
271 <!-- ##### MACRO g_thread_yield ##### -->
278 <!-- ##### MACRO g_thread_exit ##### -->
285 <!-- ##### STRUCT GMutex ##### -->
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:
293 <title>A function which will not work in a threaded environment</title>
295 int give_me_next_number ()
297 static int current_number = 0;
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;
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:
316 <title>The wrong way to write a thread-safe function</title>
318 int give_me_next_number ()
320 static int current_number = 0;
322 static GMutex * mutex = NULL;
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);
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:
343 <title>A correct thread-safe function</title>
345 static GMutex *give_me_next_number_mutex = NULL;
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 ()
351 g_assert (give_me_next_number_mutex == NULL);
352 give_me_next_number_mutex = g_mutex_new ();
355 int give_me_next_number ()
357 static int current_number = 0;
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);
370 #GStaticMutex provides a simpler and safer way of doing this.
374 If you want to use a mutex, but your code should also work without
375 calling g_thread_init() first, you can not use a #GMutex, as
376 g_mutex_new() requires that. Use a #GStaticMutex instead.
380 A #GMutex should only be accessed via the following functions.
385 All of the g_mutex_* functions are actually macros. Apart from taking
386 the addresses of them, you can however use them as if they were functions.
391 <!-- ##### FUNCTION g_mutex_new ##### -->
394 Creates a new #GMutex.
399 This function will abort, if g_thread_init() has not been called yet.
403 @Returns: a new #GMutex.
406 <!-- ##### FUNCTION g_mutex_lock ##### -->
409 Locks the #GMutex. If the #GMutex is already locked by another thread,
410 the current thread will block until the #GMutex is unlocked by the
415 This function can also be used, if g_thread_init() has not yet been
416 called and will do nothing then.
421 #GMutex is not guaranteed to be recursive, i.e. a thread might block,
422 if it already has locked the #GMutex. It will deadlock then, of
430 <!-- ##### FUNCTION g_mutex_trylock ##### -->
433 Tries to lock the #GMutex. If the #GMutex is already locked by another
434 thread, it immediately returns FALSE. Otherwise it locks the #GMutex
439 This function can also be used, if g_thread_init() has not yet been
440 called and will immediately return TRUE then.
444 @Returns: TRUE, if the #GMutex could be locked.
447 <!-- ##### FUNCTION g_mutex_unlock ##### -->
450 Unlocks the #GMutex. If another thread is blocked in a g_mutex_lock()
451 call, it will be woken and can lock the #GMutex itself. This function
452 can also be used, if g_thread_init() has not yet been called and will
459 <!-- ##### FUNCTION g_mutex_free ##### -->
462 Destroys the #GMutex.
468 <!-- ##### STRUCT GStaticMutex ##### -->
471 A #GStaticMutex works like a #GMutex, but it has one significant
472 advantage. It doesn't need to be created at run-time like a #GMutex,
473 but can be defined at compile-time. Here is a shorter, easier and
474 safer version of our give_me_next_number() example:
478 Sometimes you would like to dynamically create a mutex. If you don't
479 want to require prior calling to g_thread_init(), because your code
480 should also be usable in non-threaded programs, you are not able to
481 use g_mutex_new() and thus #GMutex, as that requires a prior call to
482 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
483 must be initialized with g_static_mutex_init() before using it and
484 freed with with g_static_mutex_free() when not needed anymore to free
485 up any allocated recourses.
490 <title>Using GStaticMutex to simplify thread-safe programming</title>
492 int give_me_next_number ()
494 static int current_number = 0;
496 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
498 g_static_mutex_lock (&mutex);
499 ret_val = current_number = calc_next_number (current_number);
500 g_static_mutex_unlock (&mutex);
508 Even though #GStaticMutex is not opaque, it should only be used with
509 the following functions, as it is defined differently on different
513 <para>All of the g_static_mutex_* functions can also be used, if
514 g_thread_init() has not yet.
519 All of the g_static_mutex_* functions are actually macros. Apart from
520 taking the addresses of them, you can however use them as if they were
526 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
529 A #GStaticMutex must be initialized with this macro, before it can be
530 used. This macro can used be to initialize a variable, but it cannot
531 be assigned to a variable. In that case you have to use
532 g_static_mutex_init().
537 <title>Initializing a GStaticMutext</title>
539 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
546 <!-- ##### FUNCTION g_static_mutex_init ##### -->
548 A #GStaticMutex must be initialized with this function, before it can
549 be used. Alternatively you can initialize it with
550 #G_STATIC_MUTEX_INIT.
553 @mutex: a #GStaticMutex to be initialized.
556 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
558 works like g_mutex_lock(), but for a #GStaticMutex.
561 @mutex: a #GStaticMutex.
564 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
567 works like g_mutex_trylock(), but for a #GStaticMutex.
570 @mutex: a #GStaticMutex.
571 @Returns: TRUE, if the #GStaticMutex could be locked.
574 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
577 works like g_mutex_unlock(), but for a #GStaticMutex.
580 @mutex: a #GStaticMutex.
583 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
586 For some operations (like g_cond_wait()) you must have a #GMutex
587 instead of a #GStaticMutex. This function will return the
588 corresponding #GMutex for every #GStaticMutex.
591 @mutex: a #GStaticMutex.
592 @Returns: the corresponding #GMutex.
595 <!-- ##### FUNCTION g_static_mutex_free ##### -->
597 Releases all resources allocated to a #GStaticMutex. You don't have to
598 call this functions for a #GStaticMutex with an unbounded lifetime,
599 i.e. objects declared 'static', but if you have a #GStaticMutex as a
600 member of a structure and the structure is freed, you should also free
604 @mutex: a #GStaticMutex.
607 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
610 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
611 with the advantage that they will expand to nothing in programs
612 compiled against a thread-disabled GLib, saving code and memory
613 there. #G_LOCK_DEFINE defines a lock. It can occur, where variable
614 definitions may occur in programs, i.e. in the first block of a
615 function or outside of functions. The @name parameter will be mangled
616 to get the name of the #GStaticMutex. This means, that you can use
617 names of existing variables as the parameter, e.g. the name of the
618 variable you intent to protect with the lock. Look at our
619 give_me_next_number() example using the G_LOCK_* macros:
624 <title>Using the G_LOCK_* convenience macros</title>
626 G_LOCK_DEFINE (current_number);
628 int give_me_next_number ()
630 static int current_number = 0;
633 G_LOCK (current_number);
634 ret_val = current_number = calc_next_number (current_number);
635 G_UNLOCK (current_number);
642 @name: the name of the lock.
645 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
648 This works like #G_LOCK_DEFINE, but it creates a static object.
651 @name: the name of the lock.
654 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
657 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
660 @name: the name of the lock.
663 <!-- ##### MACRO G_LOCK ##### -->
666 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
669 @name: the name of the lock.
672 <!-- ##### MACRO G_TRYLOCK ##### -->
675 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
678 @name: the name of the lock.
679 @Returns: TRUE, if the lock could be locked.
682 <!-- ##### MACRO G_UNLOCK ##### -->
685 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
688 @name: the name of the lock.
691 <!-- ##### STRUCT GStaticRecMutex ##### -->
700 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
707 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
715 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
724 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
732 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
741 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
750 <!-- ##### STRUCT GStaticRWLock ##### -->
762 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
769 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
777 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
786 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
794 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
802 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
811 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
819 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
827 <!-- ##### STRUCT GCond ##### -->
830 The #GCond struct is an opaque data structure to represent a
831 condition. A #GCond is an object, that threads can block on, if they
832 find a certain condition to be false. If other threads change the
833 state of this condition they can signal the #GCond, such that the
834 waiting thread is woken up.
839 <title>Using GCond to block a thread until a condition is satisfied</title>
841 GCond* data_cond = NULL; /* Must be initialized somewhere */
842 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
843 gpointer current_data = NULL;
845 void push_data (gpointer data)
847 g_mutex_lock (data_mutex);
849 g_cond_signal (data_cond);
850 g_mutex_unlock (data_mutex);
857 g_mutex_lock (data_mutex);
858 while (!current_data)
859 g_cond_wait (data_cond, data_mutex);
862 g_mutex_unlock (data_mutex);
870 Whenever a thread calls pop_data() now, it will wait until
871 current_data is non-NULL, i.e. until some other thread has called
877 It is important to use the g_cond_wait() and g_cond_timed_wait()
878 functions only inside a loop, which checks for the condition to be
879 true as it is not guaranteed that the waiting thread will find it
880 fulfilled, even if the signaling thread left the condition
881 in that state. This is because another thread can have altered the
882 condition, before the waiting thread got the chance to be woken up,
883 even if the condition itself is protected by a #GMutex, like above.
888 A #GCond should only be accessed via the following functions.
893 All of the g_cond_* functions are actually macros. Apart from taking
894 the addresses of them, you can however use them as if they were functions.
899 <!-- ##### FUNCTION g_cond_new ##### -->
902 Creates a new #GCond. This function will abort, if g_thread_init()
903 has not been called yet.
906 @Returns: a new #GCond.
909 <!-- ##### FUNCTION g_cond_signal ##### -->
911 If threads are waiting for @cond, exactly one of them is woken up. It
912 is good practice to hold the same lock as the waiting thread, while
913 calling this function, though not required.
917 This function can also be used, if g_thread_init() has
918 not yet been called and will do nothing then.
924 <!-- ##### FUNCTION g_cond_broadcast ##### -->
927 If threads are waiting for @cond, all of them are woken up. It is good
928 practice to lock the same mutex as the waiting threads, while calling
929 this function, though not required.
933 This function can also be used, if g_thread_init() has
934 not yet been called and will do nothing then.
940 <!-- ##### FUNCTION g_cond_wait ##### -->
943 Waits until this thread is woken up on the #GCond. The #GMutex is
944 unlocked before falling asleep and locked again before resuming.
948 This function can also be used, if g_thread_init() has not yet been
949 called and will immediately return then.
953 @mutex: the #GMutex, that is currently locked.
956 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
959 Waits until this thread is woken up on the #GCond, but not longer than
960 until the time, that is specified by @abs_time. The #GMutex is
961 unlocked before falling asleep and locked again before resuming.
965 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
969 This function can also be used, if g_thread_init() has not yet been
970 called and will immediately return TRUE then.
974 @mutex: the #GMutex, that is currently locked.
975 @abs_time: a #GTimeVal, determining the final time.
976 @Returns: TRUE, if the thread is woken up in time.
979 <!-- ##### FUNCTION g_cond_free ##### -->
988 <!-- ##### STRUCT GPrivate ##### -->
990 The #GPrivate struct is an opaque data structure to represent a thread
991 private data key. Threads can thereby obtain and set a pointer, which
992 is private to the current thread. Take our give_me_next_number()
993 example from above. Now we don't want current_number to be shared
994 between the threads, but to be private to each thread. This can be
998 <title>Using GPrivate for per-thread data</title>
1000 GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1001 /* with g_private_new (g_free); */
1003 int give_me_next_number ()
1005 int *current_number = g_private_get (current_number_key);
1007 if (!current_number)
1009 current_number = g_new (int,1);
1010 *current_number = 0;
1011 g_private_set (current_number_key, current_number);
1013 *current_number = calc_next_number (*current_number);
1014 return *current_number;
1021 Here the pointer belonging to the key current_number_key is read. If
1022 it is NULL, it has not been set yet. Then get memory for an integer
1023 value, assign this memory to the pointer and write the pointer
1024 back. Now we have an integer value, that is private to the current
1029 The #GPrivate struct should only be accessed via the following functions.
1034 All of the g_private_* functions are actually macros. Apart from taking
1035 the addresses of them, you can however use them as if they were functions.
1040 <!-- ##### FUNCTION g_private_new ##### -->
1043 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1044 to a destructor function. Whenever a thread ends and the corresponding
1045 pointer keyed to this instance of #GPrivate is non-NULL, the
1046 destructor is called with this pointer as the argument.
1051 The @destructor is working quite differently from @notify in
1052 g_static_private_set().
1058 A #GPrivate can not be destroyed. Reuse it instead, if you can to
1065 This function will abort, if g_thread_init() has not been called yet.
1069 @destructor: a function to handle the data keyed to #GPrivate, when a
1074 <!-- ##### FUNCTION g_private_get ##### -->
1077 Returns the pointer keyed to @private_key for the current thread. This
1078 pointer is NULL, when g_private_set() hasn't been called for the
1079 current @private_key and thread yet.
1083 This function can also be used, if g_thread_init() has not yet been
1084 called and will return the value of @private_key casted to #gpointer then.
1087 @private_key: a #GPrivate.
1088 @Returns: the corresponding pointer.
1091 <!-- ##### FUNCTION g_private_set ##### -->
1094 Sets the pointer keyed to @private_key for the current thread.
1098 This function can also be used, if g_thread_init() has not yet been
1099 called and will set @private_key to @data casted to #GPrivate* then.
1102 @private_key: a #GPrivate.
1103 @data: the new pointer.
1106 <!-- ##### STRUCT GStaticPrivate ##### -->
1109 A #GStaticPrivate works almost like a #GPrivate, but it has one
1110 significant advantage. It doesn't need to be created at run-time like
1111 a #GPrivate, but can be defined at compile-time. This is similar to
1112 the difference between #GMutex and #GStaticMutex. Now look at our
1113 give_me_next_number() example with #GStaticPrivate:
1118 <title>Using GStaticPrivate for per-thread data</title>
1120 int give_me_next_number ()
1122 static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1123 int *current_number = g_static_private_get (&current_number_key);
1125 if (!current_number)
1127 current_number = g_new (int,1);
1128 *current_number = 0;
1129 g_static_private_set (&current_number_key, current_number, g_free);
1131 *current_number = calc_next_number (*current_number);
1132 return *current_number;
1140 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1142 Every #GStaticPrivate must be initialized with this macro, before it can
1149 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1156 <!-- ##### FUNCTION g_static_private_get ##### -->
1158 Works like g_private_get() only for a #GStaticPrivate.
1162 This function also works, if g_thread_init() has not yet been called.
1165 @private_key: a #GStaticPrivate.
1166 @Returns: the corresponding pointer.
1169 <!-- ##### FUNCTION g_static_private_get_for_thread ##### -->
1179 <!-- ##### FUNCTION g_static_private_set ##### -->
1181 Sets the pointer keyed to @private_key for the current thread and the
1182 function @notify to be called with that pointer (NULL or non-NULL),
1183 whenever the pointer is set again or whenever the current thread ends.
1187 This function also works, if g_thread_init() has not yet been
1188 called. If g_thread_init() is called later, the @data keyed to
1189 @private_key will be inherited only by the main thread, i.e. the one that
1190 called g_thread_init().
1195 The @notify is working quite differently from @destructor in
1200 @private_key: a #GStaticPrivate.
1201 @data: the new pointer.
1202 @notify: a function to be called with the pointer, whenever the
1203 current thread ends or sets this pointer again.
1206 <!-- ##### FUNCTION g_static_private_set_for_thread ##### -->