1 <!-- ##### SECTION Title ##### -->
5 <!-- ##### SECTION Short_Description ##### -->
7 thread abstraction; including threads, different mutexes, conditions
8 and thread private data.
10 <!-- ##### SECTION Long_Description ##### -->
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.
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).
35 <!-- ##### SECTION See_Also ##### -->
40 <term>#GThreadPool</term>
41 <listitem><para>Thread pools.</para></listitem>
45 <term>#GAsyncQueue</term>
46 <listitem><para>Send asynchronous messages between threads.</para></listitem>
52 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
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.
64 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
67 This macro is defined, if POSIX style threads are used.
72 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
75 This macro is defined, if the Solaris thread system is used.
80 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
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.
89 <!-- ##### MACRO G_THREAD_ERROR ##### -->
91 The error domain of the GLib thread subsystem.
96 <!-- ##### ENUM GThreadError ##### -->
98 Possible errors of thread related functions.
101 @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
102 shortage. Try again later.
104 <!-- ##### STRUCT GThreadFunctions ##### -->
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.
116 This struct should only be used, if you know, what you are doing.
138 @thread_set_priority:
141 <!-- ##### FUNCTION g_thread_init ##### -->
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
152 You should only call g_thread_init() with a non-NULL parameter, if you
153 really know, what you are doing.
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().
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:
174 if (!g_thread_supported ()) g_thread_init (NULL);
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.
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.
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
200 @vtable: a function table of type #GThreadFunctions, that provides the
201 entry points to the thread system to be used
204 <!-- ##### FUNCTION g_thread_supported ##### -->
206 This function returns, whether the thread system is initialized or
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.
217 @Returns: TRUE, if the thread system is initialized
220 <!-- ##### USER_FUNCTION GThreadFunc ##### -->
222 Specifies the type of the @thread_func functions passed to
226 @value: data supplied to the thread
227 @Returns: the return value of the thread, which will be returned by
231 <!-- ##### ENUM GThreadPriority ##### -->
233 Specifies the priority of a thread.
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.
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
251 <!-- ##### STRUCT GThread ##### -->
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.
260 Resources for a joinable thread are not fully released until
261 g_thread_join() is called for that thread.
265 @joinable: is this thread joinable?
266 @bound: is this thread bound to a system thread?
267 @priority: the priority of the thread
268 @func: the function executing in that thread
269 @arg: the argument to the function
271 <!-- ##### FUNCTION g_thread_create ##### -->
273 This function creates a new thread with the priority @priority. The
274 stack gets the size @stack_size or the default value for the current
275 platform, if @stack_size is 0.
279 If @joinable is #TRUE, you can wait for this threads termination
280 calling g_thread_wait(). Otherwise the thread will just disappear, when
281 ready. If @bound is #TRUE, this thread will be scheduled in the system
282 scope, otherwise the implementation is free to do scheduling in the
283 process scope. The first variant is more expensive resource-wise, but
284 generally faster. On some systems (e.g. Linux) all threads are bound.
288 The new thread executes the function @thread_func with the argument
289 @arg. If the thread was created successfully, it is returned.
293 @error can be NULL to ignore errors, or non-NULL to report errors. The
294 error is set, if and only if the function returns #NULL.
299 It is not guaranteed, that threads with different priorities really
300 behave accordingly. On some systems (e.g. Linux) only root can increase
301 priorities. On other systems (e.g. Solaris) there doesn't seem to be
302 different scheduling for different priorities. All in all try to avoid
303 being dependent on priorities. Use %G_THREAD_PRIORITY_NORMAL here as a
308 @thread_func: a function to execute in the new thread
309 @arg: an argument to supply to the new thread
310 @stack_size: a stack size for the new thread
311 @joinable: should this thread be joinable?
312 @bound: should this thread be bound to a system thread?
313 @priority: a priority for the thread
314 @error: return location for error.
315 @Returns: the new #GThread on success
318 <!-- ##### FUNCTION g_thread_self ##### -->
320 This functions returns the #GThread corresponding to the calling thread.
323 @Returns: the current thread
326 <!-- ##### FUNCTION g_thread_join ##### -->
328 Waits until @thread finishes, i.e. the function @thread_func, as given
329 to g_thread_create, returns or g_thread_exit() is called by
330 @thread. All resources of @thread including the #GThread struct are
331 released. @thread must have been created with @joinable=#TRUE in
332 g_thread_create(). The value returned by @thread_func or given to
333 g_thread_exit() by @thread is returned by this function.
336 @thread: a #GThread to be waited for
337 @Returns: the return value of the thread
340 <!-- ##### FUNCTION g_thread_set_priority ##### -->
342 Change the priority of @thread to @priority.
347 It is not guaranteed, that threads with different priorities really
348 behave accordingly. On some systems (e.g. Linux) only root can increase
349 priorities. On other systems (e.g. Solaris) there doesn't seem to be
350 different scheduling for different priorities. All in all try to avoid
351 being dependent on priorities.
356 @priority: a new priority for @thread
359 <!-- ##### FUNCTION g_thread_yield ##### -->
361 Give way to other threads waiting to be scheduled.
365 This function is often used as a method to make busy wait less
366 evil. But in most cases, you will encounter, there are better methods
367 to do that. So in general you shouldn't use that function.
372 <!-- ##### FUNCTION g_thread_exit ##### -->
374 Exit the current thread. If another thread is waiting for that thread
375 using g_thread_join() and the current thread is joinable, the waiting
376 thread will be woken up and getting @retval as the return value of
377 g_thread_join(). If the current thread is not joinable, @retval is
384 g_thread_join (retval);
390 is equivalent to calling
402 in the function @thread_func, as given to g_thread_create().
407 Never call g_thread_exit from within a thread of a #GThreadPool, as
408 that will mess up the bookkeeping and lead to funny and unwanted
413 @retval: the return value of this thread
416 <!-- ##### STRUCT GMutex ##### -->
419 The #GMutex struct is an opaque data structure to represent a mutex
420 (mutual exclusion). It can be used to protect data against shared
421 access. Take for example the following function:
424 <title>A function which will not work in a threaded environment</title>
426 int give_me_next_number ()
428 static int current_number = 0;
430 /* now do a very complicated calculation to calculate the new number,
431 this might for example be a random number generator */
432 current_number = calc_next_number (current_number);
433 return current_number;
440 It is easy to see, that this won't work in a multi-threaded
441 application. There current_number must be protected against shared
442 access. A first naive implementation would be:
447 <title>The wrong way to write a thread-safe function</title>
449 int give_me_next_number ()
451 static int current_number = 0;
453 static GMutex * mutex = NULL;
456 mutex = g_mutex_new ();
457 g_mutex_lock (mutex);
458 ret_val = current_number = calc_next_number (current_number);
459 g_mutex_unlock (mutex);
467 This looks like it would work, but there is a race condition while
468 constructing the mutex and this code cannot work reliable. So please do
469 not use such constructs in your own programs. One working solution is:
474 <title>A correct thread-safe function</title>
476 static GMutex *give_me_next_number_mutex = NULL;
478 /* this function must be called before any call to give_me_next_number ()
479 it must be called exactly once. */
480 void init_give_me_next_number ()
482 g_assert (give_me_next_number_mutex == NULL);
483 give_me_next_number_mutex = g_mutex_new ();
486 int give_me_next_number ()
488 static int current_number = 0;
491 g_mutex_lock (give_me_next_number_mutex);
492 ret_val = current_number = calc_next_number (current_number);
493 g_mutex_unlock (give_me_next_number_mutex);
501 #GStaticMutex provides a simpler and safer way of doing this.
505 If you want to use a mutex, but your code should also work without
506 calling g_thread_init() first, you can not use a #GMutex, as
507 g_mutex_new() requires that. Use a #GStaticMutex instead.
511 A #GMutex should only be accessed via the following functions.
516 All of the g_mutex_* functions are actually macros. Apart from taking
517 the addresses of them, you can however use them as if they were functions.
522 <!-- ##### FUNCTION g_mutex_new ##### -->
525 Creates a new #GMutex.
530 This function will abort, if g_thread_init() has not been called yet.
534 @Returns: a new #GMutex
537 <!-- ##### FUNCTION g_mutex_lock ##### -->
540 Locks @mutex. If @mutex is already locked by another thread, the
541 current thread will block until @mutex is unlocked by the other
546 This function can also be used, if g_thread_init() has not yet been
547 called and will do nothing then.
552 #GMutex is not recursive, i.e. a thread will deadlock, if it already
553 has locked @mutex while calling g_mutex_lock(). Use
554 #GStaticRecMutex instead, if you need recursive mutexes.
561 <!-- ##### FUNCTION g_mutex_trylock ##### -->
564 Tries to lock @mutex. If @mutex is already locked by another
565 thread, it immediately returns FALSE. Otherwise it locks @mutex
570 This function can also be used, if g_thread_init() has not yet been
571 called and will immediately return TRUE then.
576 #GMutex is not recursive, i.e. g_mutex_trylock() will return FALSE,
577 if the current thread already has locked @mutex. Use
578 #GStaticRecMutex instead, if you need recursive mutexes.
583 @Returns: TRUE, if @mutex could be locked
586 <!-- ##### FUNCTION g_mutex_unlock ##### -->
589 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
590 for @mutex, it will be woken and can lock @mutex itself.
594 This function can also be used, if g_thread_init() has not yet been
595 called and will do nothing then.
601 <!-- ##### FUNCTION g_mutex_free ##### -->
610 <!-- ##### STRUCT GStaticMutex ##### -->
613 A #GStaticMutex works like a #GMutex, but it has one significant
614 advantage. It doesn't need to be created at run-time like a #GMutex,
615 but can be defined at compile-time. Here is a shorter, easier and
616 safer version of our give_me_next_number() example:
621 <title>Using GStaticMutex to simplify thread-safe programming</title>
623 int give_me_next_number ()
625 static int current_number = 0;
627 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
629 g_static_mutex_lock (&mutex);
630 ret_val = current_number = calc_next_number (current_number);
631 g_static_mutex_unlock (&mutex);
639 Sometimes you would like to dynamically create a mutex. If you don't
640 want to require prior calling to g_thread_init(), because your code
641 should also be usable in non-threaded programs, you are not able to
642 use g_mutex_new() and thus #GMutex, as that requires a prior call to
643 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
644 must be initialized with g_static_mutex_init() before using it and
645 freed with with g_static_mutex_free() when not needed anymore to free
646 up any allocated recourses.
650 Even though #GStaticMutex is not opaque, it should only be used with
651 the following functions, as it is defined differently on different
656 All of the g_static_mutex_* functions can also be used, if
657 g_thread_init() has not yet been called.
662 All of the g_static_mutex_* functions are actually macros. Apart from
663 taking the addresses of them, you can however use them as if they were
669 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
672 A #GStaticMutex must be initialized with this macro, before it can be
673 used. This macro can used be to initialize a variable, but it cannot
674 be assigned to a variable. In that case you have to use
675 g_static_mutex_init().
681 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
688 <!-- ##### FUNCTION g_static_mutex_init ##### -->
690 Initializes @mutex. Alternatively you can initialize it with
691 #G_STATIC_MUTEX_INIT.
694 @mutex: a #GStaticMutex to be initialized
697 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
699 Works like g_mutex_lock(), but for a #GStaticMutex.
702 @mutex: a #GStaticMutex
705 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
708 Works like g_mutex_trylock(), but for a #GStaticMutex.
711 @mutex: a #GStaticMutex
712 @Returns: TRUE, if the #GStaticMutex could be locked
715 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
718 Works like g_mutex_unlock(), but for a #GStaticMutex.
721 @mutex: a #GStaticMutex
724 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
727 For some operations (like g_cond_wait()) you must have a #GMutex
728 instead of a #GStaticMutex. This function will return the
729 corresponding #GMutex for @mutex.
732 @mutex: a #GStaticMutex
733 @Returns: the #GMutex corresponding to @mutex
736 <!-- ##### FUNCTION g_static_mutex_free ##### -->
738 Releases all resources allocated to @mutex.
742 You don't have to call this functions for a #GStaticMutex with an
743 unbounded lifetime, i.e. objects declared 'static', but if you have a
744 #GStaticMutex as a member of a structure and the structure is freed,
745 you should also free the #GStaticMutex.
748 @mutex: a #GStaticMutex to be freed
751 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
754 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
755 with the advantage that they will expand to nothing in programs
756 compiled against a thread-disabled GLib, saving code and memory
757 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
758 definitions may appear in programs, i.e. in the first block of a
759 function or outside of functions. The @name parameter will be mangled
760 to get the name of the #GStaticMutex. This means, that you can use
761 names of existing variables as the parameter, e.g. the name of the
762 variable you intent to protect with the lock. Look at our
763 give_me_next_number() example using the G_LOCK_* macros:
768 <title>Using the G_LOCK_* convenience macros</title>
770 G_LOCK_DEFINE (current_number);
772 int give_me_next_number ()
774 static int current_number = 0;
777 G_LOCK (current_number);
778 ret_val = current_number = calc_next_number (current_number);
779 G_UNLOCK (current_number);
786 @name: the name of the lock
789 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
792 This works like #G_LOCK_DEFINE, but it creates a static object.
795 @name: the name of the lock
798 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
801 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
804 @name: the name of the lock
807 <!-- ##### MACRO G_LOCK ##### -->
810 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
813 @name: the name of the lock
816 <!-- ##### MACRO G_TRYLOCK ##### -->
819 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
822 @name: the name of the lock
823 @Returns: TRUE, if the lock could be locked
826 <!-- ##### MACRO G_UNLOCK ##### -->
829 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
832 @name: the name of the lock
835 <!-- ##### STRUCT GStaticRecMutex ##### -->
837 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
838 multiple times by one thread. If you enter it n times, however, you
839 have to unlock it n times again to let other threads lock it. An
840 exception is the function g_static_rec_mutex_unlock_full(), that
841 allows you to unlock a #GStaticRecMutex completely returning the depth,
842 i.e. the number of times this mutex was locked. The depth can later be
843 used to restore the state by calling g_static_rec_mutex_lock_full().
847 Even though #GStaticRecMutex is not opaque, it should only be used with
848 the following functions.
852 All of the g_static_rec_mutex_* functions can also be used, if
853 g_thread_init() has not been called.
860 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
862 A #GStaticRecMutex must be initialized with this macro, before it can
863 be used. This macro can used be to initialize a variable, but it
864 cannot be assigned to a variable. In that case you have to use
865 g_static_rec_mutex_init().
871 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
878 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
880 A #GStaticRecMutex must be initialized with this function, before it
881 can be used. Alternatively you can initialize it with
882 #G_STATIC_REC_MUTEX_INIT.
885 @mutex: a #GStaticRecMutex to be initialized
888 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
890 Locks @mutex. If @mutex is already locked by another thread, the
891 current thread will block until @mutex is unlocked by the other
892 thread. If @mutex is already locked by the calling thread, this
893 functions increases the depth of @mutex and returns immediately.
896 @mutex: a #GStaticRecMutex to lock
899 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
901 Tries to lock @mutex. If @mutex is already locked by another thread,
902 it immediately returns #FALSE. Otherwise it locks @mutex and returns
903 #TRUE. If @mutex is already locked by the calling thread, this
904 functions increases the depth of @mutex and immediately returns #TRUE.
907 @mutex: a #GStaticRecMutex to lock
908 @Returns: TRUE, if @mutex could be locked
911 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
913 Unlocks @mutex. Another threads can, however, only lock @mutex when it
914 has been unlocked as many times, as it had been locked before. If
915 @mutex is completely unlocked and another thread is blocked in a
916 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
920 @mutex: a #GStaticRecMutex to unlock
923 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
925 Works like calling g_static_rec_mutex_lock() for @mutex n times.
928 @mutex: a #GStaticRecMutex to lock
929 @depth: number of times this mutex has to be unlocked to be completely unlocked
932 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
934 Completely unlocks @mutex. If another thread is blocked in a
935 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
936 lock @mutex itself. This function returns the number of times, that
937 @mutex has been locked by the current thread. To restore the state
938 before the call to g_static_rec_mutex_unlock_full() you can call
939 g_static_rec_mutex_lock_full() with the depth returned by this
943 @mutex: a #GStaticRecMutex to completely unlock
944 @Returns: number of times @mutex has been locked by the current thread
947 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
949 Releases all resources allocated to a #GStaticRecMutex.
953 You don't have to call this functions for a #GStaticRecMutex with an
954 unbounded lifetime, i.e. objects declared 'static', but if you have a
955 #GStaticRecMutex as a member of a structure and the structure is
956 freed, you should also free the #GStaticRecMutex.
959 @mutex: a #GStaticRecMutex to be freed
962 <!-- ##### STRUCT GStaticRWLock ##### -->
964 The #GStaticRWLock struct represents a read-write lock. A read-write
965 lock can be used for protecting data, that some portions of code only
966 read from, while others also write. In such situations it is
967 desirable, that several readers can read at once, whereas of course
968 only one writer may write at a time. Take a look at the following
972 <title>An array with access functions</title>
974 GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
978 gpointer my_array_get (guint index)
980 gpointer retval = NULL;
985 g_static_rw_lock_reader_lock (&rwlock);
987 if (index < array->len)
988 retval = g_ptr_array_index (array, index);
990 g_static_rw_lock_reader_unlock (&rwlock);
995 void my_array_set (guint index, gpointer data)
997 g_static_rw_lock_writer_lock (&rwlock);
1000 array = g_ptr_array_new ();
1002 if (index >= array->len)
1003 g_ptr_array_set_size (array, index+1);
1005 g_ptr_array_index (array, index) = data;
1007 g_static_rw_lock_writer_unlock (&rwlock);
1014 This example shows an array, which can be accessed by many readers
1015 (the my_array_get function) simultaneously, whereas the writers (the
1016 my_array_set function) only will be allowed once a time and only if no
1017 readers currently access the array. This is because of the potentially
1018 dangerous resizing of the array. Using that functions is fully
1019 multi-thread safe now.
1023 Most of the time the writers should have precedence of readers. That
1024 means for this implementation, that as soon as a writer wants to lock
1025 the data, no other reader is allowed to lock the data, whereas of
1026 course the readers, that already have locked the data are allowed to
1027 finish their operation. As soon as the last reader unlocks the data,
1028 the writer will lock it.
1032 Even though #GStaticRWLock is not opaque, it should only be used with
1033 the following functions.
1037 All of the g_static_rw_lock_* functions can also be used, if
1038 g_thread_init() has not been called.
1043 A read-write lock has a higher overhead as a mutex. For example both
1044 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1045 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1046 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1047 #GStaticMutex. So only data structures, that are accessed by multiple
1048 readers, which keep the lock for a considerable time justify a
1049 #GStaticRWLock. The above example most probably would fare better with
1061 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1063 A #GStaticRWLock must be initialized with this macro, before it can
1064 be used. This macro can used be to initialize a variable, but it
1065 cannot be assigned to a variable. In that case you have to use
1066 g_static_rw_lock_init().
1072 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1079 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1081 A #GStaticRWLock must be initialized with this function, before it can
1082 be used. Alternatively you can initialize it with
1083 #G_STATIC_RW_LOCK_INIT.
1086 @lock: a #GStaticRWLock to be initialized
1089 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1091 Locks @lock for reading. There may be unlimited concurrent locks for
1092 reading of a #GStaticRWLock at the same time. If @lock is already
1093 locked for writing by another thread or if another thread is already
1094 waiting to lock @lock for writing, this function will block until
1095 @lock is unlocked by the other writing thread and no other writing
1096 threads want to lock @lock. This lock has to be unlocked by
1097 g_static_rw_lock_reader_unlock().
1101 #GStaticRWLock in general is not recursive, but as there may be
1102 unlimited concurrent locks for reading, it effectively is for
1103 recursive for reading, but for reading only. Locking for writing after
1104 locking for reading will deadlock, the same holds true for the
1108 @lock: a #GStaticRWLock to lock for reading
1111 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1113 Tries to lock @lock for reading. If @lock is already locked for
1114 writing by another thread or if another thread is already waiting to
1115 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1116 locks @lock for reading and returns TRUE. This lock has to be unlocked
1117 by g_static_rw_lock_reader_unlock().
1120 @lock: a #GStaticRWLock to lock for reading
1121 @Returns: TRUE, if @lock could be locked for reading
1124 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1126 Unlocks @lock. If a thread waits to lock @lock for writing and all
1127 locks for reading have been unlocked, the waiting thread is woken up
1128 and can lock @lock for writing.
1131 @lock: a #GStaticRWLock to unlock after reading
1134 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1136 Locks @lock for writing. If @lock is already locked for writing or
1137 reading by other threads, this function will block until @lock is
1138 completely unlocked and then lock @lock for writing. While this
1139 functions waits to lock @lock, no other thread can lock @lock for
1140 reading. When @lock is locked for writing, no other thread can lock
1141 @lock (neither for reading nor writing). This lock has to be unlocked
1142 by g_static_rw_lock_writer_unlock().
1145 @lock: a #GStaticRWLock to lock for writing
1148 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1150 Tries to lock @lock for writing. If @lock is already locked (for
1151 either reading or writing) by another thread, it immediately returns
1152 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1153 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1156 @lock: a #GStaticRWLock to lock for writing
1157 @Returns: TRUE, if @lock could be locked for writing
1160 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
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. If no thread waits to lock @lock for
1165 writing and threads wait to lock @lock for reading, the waiting
1166 threads are woken up and can lock @lock for reading.
1169 @lock: a #GStaticRWLock to unlock after writing
1172 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1174 Releases all resources allocated to @lock.
1178 You don't have to call this functions for a #GStaticRWLock with an
1179 unbounded lifetime, i.e. objects declared 'static', but if you have a
1180 #GStaticRWLock as a member of a structure and the structure is freed,
1181 you should also free the #GStaticRWLock.
1184 @lock: a #GStaticRWLock to be freed
1187 <!-- ##### STRUCT GCond ##### -->
1190 The #GCond struct is an opaque data structure to represent a
1191 condition. A #GCond is an object, that threads can block on, if they
1192 find a certain condition to be false. If other threads change the
1193 state of this condition they can signal the #GCond, such that the
1194 waiting thread is woken up.
1199 <title>Using GCond to block a thread until a condition is satisfied</title>
1201 GCond* data_cond = NULL; /* Must be initialized somewhere */
1202 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1203 gpointer current_data = NULL;
1205 void push_data (gpointer data)
1207 g_mutex_lock (data_mutex);
1208 current_data = data;
1209 g_cond_signal (data_cond);
1210 g_mutex_unlock (data_mutex);
1213 gpointer pop_data ()
1217 g_mutex_lock (data_mutex);
1218 while (!current_data)
1219 g_cond_wait (data_cond, data_mutex);
1220 data = current_data;
1221 current_data = NULL;
1222 g_mutex_unlock (data_mutex);
1230 Whenever a thread calls pop_data() now, it will wait until
1231 current_data is non-NULL, i.e. until some other thread has called
1237 It is important to use the g_cond_wait() and g_cond_timed_wait()
1238 functions only inside a loop, which checks for the condition to be
1239 true as it is not guaranteed that the waiting thread will find it
1240 fulfilled, even if the signaling thread left the condition
1241 in that state. This is because another thread can have altered the
1242 condition, before the waiting thread got the chance to be woken up,
1243 even if the condition itself is protected by a #GMutex, like above.
1248 A #GCond should only be accessed via the following functions.
1253 All of the g_cond_* functions are actually macros. Apart from taking
1254 the addresses of them, you can however use them as if they were functions.
1259 <!-- ##### FUNCTION g_cond_new ##### -->
1262 Creates a new #GCond. This function will abort, if g_thread_init()
1263 has not been called yet.
1266 @Returns: a new #GCond
1269 <!-- ##### FUNCTION g_cond_signal ##### -->
1271 If threads are waiting for @cond, exactly one of them is woken up. It
1272 is good practice to hold the same lock as the waiting thread, while
1273 calling this function, though not required.
1277 This function can also be used, if g_thread_init() has
1278 not yet been called and will do nothing then.
1284 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1287 If threads are waiting for @cond, all of them are woken up. It is good
1288 practice to lock the same mutex as the waiting threads, while calling
1289 this function, though not required.
1293 This function can also be used, if g_thread_init() has
1294 not yet been called and will do nothing then.
1300 <!-- ##### FUNCTION g_cond_wait ##### -->
1303 Waits until this thread is woken up on @cond. The @mutex is unlocked
1304 before falling asleep and locked again before resuming.
1308 This function can also be used, if g_thread_init() has not yet been
1309 called and will immediately return then.
1313 @mutex: a #GMutex, that is currently locked
1316 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1319 Waits until this thread is woken up on @cond, but not longer than
1320 until the time, that is specified by @abs_time. The @mutex is
1321 unlocked before falling asleep and locked again before resuming.
1325 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1329 This function can also be used, if g_thread_init() has not yet been
1330 called and will immediately return TRUE then.
1334 @mutex: a #GMutex, that is currently locked
1335 @abs_time: a #GTimeVal, determining the final time
1336 @Returns: TRUE, if the thread is woken up in time
1339 <!-- ##### FUNCTION g_cond_free ##### -->
1342 Destroys the #GCond.
1348 <!-- ##### STRUCT GPrivate ##### -->
1350 The #GPrivate struct is an opaque data structure to represent a thread
1351 private data key. Threads can thereby obtain and set a pointer, which
1352 is private to the current thread. Take our give_me_next_number()
1353 example from above. Now we don't want current_number to be shared
1354 between the threads, but to be private to each thread. This can be
1358 <title>Using GPrivate for per-thread data</title>
1360 GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1361 /* with g_private_new (g_free); */
1363 int give_me_next_number ()
1365 int *current_number = g_private_get (current_number_key);
1367 if (!current_number)
1369 current_number = g_new (int,1);
1370 *current_number = 0;
1371 g_private_set (current_number_key, current_number);
1373 *current_number = calc_next_number (*current_number);
1374 return *current_number;
1381 Here the pointer belonging to the key current_number_key is read. If
1382 it is NULL, it has not been set yet. Then get memory for an integer
1383 value, assign this memory to the pointer and write the pointer
1384 back. Now we have an integer value, that is private to the current
1389 The #GPrivate struct should only be accessed via the following functions.
1394 All of the g_private_* functions are actually macros. Apart from taking
1395 the addresses of them, you can however use them as if they were functions.
1400 <!-- ##### FUNCTION g_private_new ##### -->
1403 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1404 to a destructor function. Whenever a thread ends and the corresponding
1405 pointer keyed to this instance of #GPrivate is non-NULL, the
1406 destructor is called with this pointer as the argument.
1411 @destructor is working quite differently from @notify in
1412 g_static_private_set().
1418 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1419 shortage or use #GStaticPrivate.
1425 This function will abort, if g_thread_init() has not been called yet.
1429 @destructor: a function to handle the data keyed to #GPrivate, when a
1431 @Returns: a new #GPrivate
1434 <!-- ##### FUNCTION g_private_get ##### -->
1437 Returns the pointer keyed to @private_key for the current thread. This
1438 pointer is NULL, when g_private_set() hasn't been called for the
1439 current @private_key and thread yet.
1443 This function can also be used, if g_thread_init() has not yet been
1444 called and will return the value of @private_key casted to #gpointer then.
1447 @private_key: a #GPrivate
1448 @Returns: the corresponding pointer
1451 <!-- ##### FUNCTION g_private_set ##### -->
1454 Sets the pointer keyed to @private_key for the current thread.
1458 This function can also be used, if g_thread_init() has not yet been
1459 called and will set @private_key to @data casted to #GPrivate* then.
1462 @private_key: a #GPrivate
1463 @data: the new pointer
1466 <!-- ##### STRUCT GStaticPrivate ##### -->
1469 A #GStaticPrivate works almost like a #GPrivate, but it has one
1470 significant advantage. It doesn't need to be created at run-time like
1471 a #GPrivate, but can be defined at compile-time. This is similar to
1472 the difference between #GMutex and #GStaticMutex. Now look at our
1473 give_me_next_number() example with #GStaticPrivate:
1478 <title>Using GStaticPrivate for per-thread data</title>
1480 int give_me_next_number ()
1482 static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1483 int *current_number = g_static_private_get (&current_number_key);
1485 if (!current_number)
1487 current_number = g_new (int,1);
1488 *current_number = 0;
1489 g_static_private_set (&current_number_key, current_number, g_free);
1491 *current_number = calc_next_number (*current_number);
1492 return *current_number;
1500 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1502 Every #GStaticPrivate must be initialized with this macro, before it can
1509 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1516 <!-- ##### FUNCTION g_static_private_init ##### -->
1518 Initializes @private_key. Alternatively you can initialize it with
1519 #G_STATIC_PRIVATE_INIT.
1522 @private_key: a #GStaticPrivate to be initialized
1525 <!-- ##### FUNCTION g_static_private_get ##### -->
1527 Works like g_private_get() only for a #GStaticPrivate.
1531 This function also works, if g_thread_init() has not yet been called.
1534 @private_key: a #GStaticPrivate
1535 @Returns: the corresponding pointer
1538 <!-- ##### FUNCTION g_static_private_set ##### -->
1540 Sets the pointer keyed to @private_key for the current thread and the
1541 function @notify to be called with that pointer (NULL or non-NULL),
1542 whenever the pointer is set again or whenever the current thread ends.
1546 This function also works, if g_thread_init() has not yet been
1547 called. If g_thread_init() is called later, the @data keyed to
1548 @private_key will be inherited only by the main thread, i.e. the one that
1549 called g_thread_init().
1554 @notify is working quite differently from @destructor in
1559 @private_key: a #GStaticPrivate
1560 @data: the new pointer
1561 @notify: a function to be called with the pointer, whenever the
1562 current thread ends or sets this pointer again
1565 <!-- ##### FUNCTION g_static_private_free ##### -->
1567 Releases all resources allocated to @private_key.
1571 You don't have to call this functions for a #GStaticPrivate with an
1572 unbounded lifetime, i.e. objects declared 'static', but if you have a
1573 #GStaticPrivate as a member of a structure and the structure is freed,
1574 you should also free the #GStaticPrivate.
1577 @private_key: a #GStaticPrivate to be freed