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 the #GMutex 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.
575 @Returns: TRUE, if @mutex could be locked
578 <!-- ##### FUNCTION g_mutex_unlock ##### -->
581 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
582 for @mutex, it will be woken and can lock @mutex itself.
586 This function can also be used, if g_thread_init() has not yet been
587 called and will do nothing then.
593 <!-- ##### FUNCTION g_mutex_free ##### -->
602 <!-- ##### STRUCT GStaticMutex ##### -->
605 A #GStaticMutex works like a #GMutex, but it has one significant
606 advantage. It doesn't need to be created at run-time like a #GMutex,
607 but can be defined at compile-time. Here is a shorter, easier and
608 safer version of our give_me_next_number() example:
613 <title>Using GStaticMutex to simplify thread-safe programming</title>
615 int give_me_next_number ()
617 static int current_number = 0;
619 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
621 g_static_mutex_lock (&mutex);
622 ret_val = current_number = calc_next_number (current_number);
623 g_static_mutex_unlock (&mutex);
631 Sometimes you would like to dynamically create a mutex. If you don't
632 want to require prior calling to g_thread_init(), because your code
633 should also be usable in non-threaded programs, you are not able to
634 use g_mutex_new() and thus #GMutex, as that requires a prior call to
635 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
636 must be initialized with g_static_mutex_init() before using it and
637 freed with with g_static_mutex_free() when not needed anymore to free
638 up any allocated recourses.
642 Even though #GStaticMutex is not opaque, it should only be used with
643 the following functions, as it is defined differently on different
648 All of the g_static_mutex_* functions can also be used, if
649 g_thread_init() has not yet been called.
654 All of the g_static_mutex_* functions are actually macros. Apart from
655 taking the addresses of them, you can however use them as if they were
661 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
664 A #GStaticMutex must be initialized with this macro, before it can be
665 used. This macro can used be to initialize a variable, but it cannot
666 be assigned to a variable. In that case you have to use
667 g_static_mutex_init().
673 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
680 <!-- ##### FUNCTION g_static_mutex_init ##### -->
682 Initializes @mutex. Alternatively you can initialize it with
683 #G_STATIC_MUTEX_INIT.
686 @mutex: a #GStaticMutex to be initialized
689 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
691 Works like g_mutex_lock(), but for a #GStaticMutex.
694 @mutex: a #GStaticMutex
697 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
700 Works like g_mutex_trylock(), but for a #GStaticMutex.
703 @mutex: a #GStaticMutex
704 @Returns: TRUE, if the #GStaticMutex could be locked
707 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
710 Works like g_mutex_unlock(), but for a #GStaticMutex.
713 @mutex: a #GStaticMutex
716 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
719 For some operations (like g_cond_wait()) you must have a #GMutex
720 instead of a #GStaticMutex. This function will return the
721 corresponding #GMutex for @mutex.
724 @mutex: a #GStaticMutex
725 @Returns: the #GMutex corresponding to @mutex
728 <!-- ##### FUNCTION g_static_mutex_free ##### -->
730 Releases all resources allocated to @mutex.
734 You don't have to call this functions for a #GStaticMutex with an
735 unbounded lifetime, i.e. objects declared 'static', but if you have a
736 #GStaticMutex as a member of a structure and the structure is freed,
737 you should also free the #GStaticMutex.
740 @mutex: a #GStaticMutex to be freed
743 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
746 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
747 with the advantage that they will expand to nothing in programs
748 compiled against a thread-disabled GLib, saving code and memory
749 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
750 definitions may appear in programs, i.e. in the first block of a
751 function or outside of functions. The @name parameter will be mangled
752 to get the name of the #GStaticMutex. This means, that you can use
753 names of existing variables as the parameter, e.g. the name of the
754 variable you intent to protect with the lock. Look at our
755 give_me_next_number() example using the G_LOCK_* macros:
760 <title>Using the G_LOCK_* convenience macros</title>
762 G_LOCK_DEFINE (current_number);
764 int give_me_next_number ()
766 static int current_number = 0;
769 G_LOCK (current_number);
770 ret_val = current_number = calc_next_number (current_number);
771 G_UNLOCK (current_number);
778 @name: the name of the lock
781 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
784 This works like #G_LOCK_DEFINE, but it creates a static object.
787 @name: the name of the lock
790 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
793 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
796 @name: the name of the lock
799 <!-- ##### MACRO G_LOCK ##### -->
802 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
805 @name: the name of the lock
808 <!-- ##### MACRO G_TRYLOCK ##### -->
811 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
814 @name: the name of the lock
815 @Returns: TRUE, if the lock could be locked
818 <!-- ##### MACRO G_UNLOCK ##### -->
821 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
824 @name: the name of the lock
827 <!-- ##### STRUCT GStaticRecMutex ##### -->
829 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
830 multiple times by one thread. If you enter it n times, however, you
831 have to unlock it n times again to let other threads lock it. An
832 exception is the function g_static_rec_mutex_unlock_full(), that
833 allows you to unlock a #GStaticRecMutex completely returning the depth,
834 i.e. the number of times this mutex was locked. The depth can later be
835 used to restore the state by calling g_static_rec_mutex_lock_full().
839 Even though #GStaticRecMutex is not opaque, it should only be used with
840 the following functions.
844 All of the g_static_rec_mutex_* functions can also be used, if
845 g_thread_init() has not been called.
852 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
854 A #GStaticRecMutex must be initialized with this macro, before it can
855 be used. This macro can used be to initialize a variable, but it
856 cannot be assigned to a variable. In that case you have to use
857 g_static_rec_mutex_init().
863 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
870 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
872 A #GStaticRecMutex must be initialized with this function, before it
873 can be used. Alternatively you can initialize it with
874 #G_STATIC_REC_MUTEX_INIT.
877 @mutex: a #GStaticRecMutex to be initialized
880 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
882 Locks @mutex. If @mutex is already locked by another thread, the
883 current thread will block until @mutex is unlocked by the other
884 thread. If @mutex is already locked by the calling thread, this
885 functions increases the depth of @mutex and returns immediately.
888 @mutex: a #GStaticRecMutex to lock
891 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
893 Tries to lock @mutex. If @mutex is already locked by another thread,
894 it immediately returns #FALSE. Otherwise it locks @mutex and returns
895 #TRUE. If @mutex is already locked by the calling thread, this
896 functions increases the depth of @mutex and immediately returns #TRUE.
899 @mutex: a #GStaticRecMutex to lock
900 @Returns: TRUE, if @mutex could be locked
903 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
905 Unlocks @mutex. Another threads can, however, only lock @mutex when it
906 has been unlocked as many times, as it had been locked before. If
907 @mutex is completely unlocked and another thread is blocked in a
908 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
912 @mutex: a #GStaticRecMutex to unlock
915 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
917 Works like calling g_static_rec_mutex_lock() for @mutex n times.
920 @mutex: a #GStaticRecMutex to lock
921 @depth: number of times this mutex has to be unlocked to be completely unlocked
924 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
926 Completely unlocks @mutex. If another thread is blocked in a
927 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
928 lock @mutex itself. This function returns the number of times, that
929 @mutex has been locked by the current thread. To restore the state
930 before the call to g_static_rec_mutex_unlock_full() you can call
931 g_static_rec_mutex_lock_full() with the depth returned by this
935 @mutex: a #GStaticRecMutex to completely unlock
936 @Returns: number of times @mutex has been locked by the current thread
939 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
941 Releases all resources allocated to a #GStaticRecMutex.
945 You don't have to call this functions for a #GStaticRecMutex with an
946 unbounded lifetime, i.e. objects declared 'static', but if you have a
947 #GStaticRecMutex as a member of a structure and the structure is
948 freed, you should also free the #GStaticRecMutex.
951 @mutex: a #GStaticRecMutex to be freed
954 <!-- ##### STRUCT GStaticRWLock ##### -->
956 The #GStaticRWLock struct represents a read-write lock. A read-write
957 lock can be used for protecting data, that some portions of code only
958 read from, while others also write. In such situations it is
959 desirable, that several readers can read at once, whereas of course
960 only one writer may write at a time. Take a look at the following
964 <title>An array with access functions</title>
966 GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
970 gpointer my_array_get (guint index)
972 gpointer retval = NULL;
977 g_static_rw_lock_reader_lock (&rwlock);
979 if (index < array->len)
980 retval = g_ptr_array_index (array, index);
982 g_static_rw_lock_reader_unlock (&rwlock);
987 void my_array_set (guint index, gpointer data)
989 g_static_rw_lock_writer_lock (&rwlock);
992 array = g_ptr_array_new ();
994 if (index >= array->len)
995 g_ptr_array_set_size (array, index+1);
997 g_ptr_array_index (array, index) = data;
999 g_static_rw_lock_writer_unlock (&rwlock);
1006 This example shows an array, which can be accessed by many readers
1007 (the my_array_get function) simultaneously, whereas the writers (the
1008 my_array_set function) only will be allowed once a time and only if no
1009 readers currently access the array. This is because of the potentially
1010 dangerous resizing of the array. Using that functions is fully
1011 multi-thread safe now.
1015 Most of the time the writers should have precedence of readers. That
1016 means for this implementation, that as soon as a writer wants to lock
1017 the data, no other reader is allowed to lock the data, whereas of
1018 course the readers, that already have locked the data are allowed to
1019 finish their operation. As soon as the last reader unlocks the data,
1020 the writer will lock it.
1024 Even though #GStaticRWLock is not opaque, it should only be used with
1025 the following functions.
1029 All of the g_static_rw_lock_* functions can also be used, if
1030 g_thread_init() has not been called.
1035 A read-write lock has a higher overhead as a mutex. For example both
1036 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1037 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1038 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1039 #GStaticMutex. So only data structures, that are accessed by multiple
1040 readers, which keep the lock for a considerable time justify a
1041 #GStaticRWLock. The above example most probably would fare better with
1053 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1055 A #GStaticRWLock must be initialized with this macro, before it can
1056 be used. This macro can used be to initialize a variable, but it
1057 cannot be assigned to a variable. In that case you have to use
1058 g_static_rw_lock_init().
1064 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1071 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1073 A #GStaticRWLock must be initialized with this function, before it can
1074 be used. Alternatively you can initialize it with
1075 #G_STATIC_RW_LOCK_INIT.
1078 @lock: a #GStaticRWLock to be initialized
1081 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1083 Locks @lock for reading. There may be unlimited concurrent locks for
1084 reading of a #GStaticRWLock at the same time. If @lock is already
1085 locked for writing by another thread or if another thread is already
1086 waiting to lock @lock for writing, this function will block until
1087 @lock is unlocked by the other writing thread and no other writing
1088 threads want to lock @lock. This lock has to be unlocked by
1089 g_static_rw_lock_reader_unlock().
1093 #GStaticRWLock in general is not recursive, but as there may be
1094 unlimited concurrent locks for reading, it effectively is for
1095 recursive for reading, but for reading only. Locking for writing after
1096 locking for reading will deadlock, the same holds true for the
1100 @lock: a #GStaticRWLock to lock for reading
1103 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1105 Tries to lock @lock for reading. If @lock is already locked for
1106 writing by another thread or if another thread is already waiting to
1107 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1108 locks @lock for reading and returns TRUE. This lock has to be unlocked
1109 by g_static_rw_lock_reader_unlock().
1112 @lock: a #GStaticRWLock to lock for reading
1113 @Returns: TRUE, if @lock could be locked for reading
1116 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1118 Unlocks @lock. If a thread waits to lock @lock for writing and all
1119 locks for reading have been unlocked, the waiting thread is woken up
1120 and can lock @lock for writing.
1123 @lock: a #GStaticRWLock to unlock after reading
1126 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1128 Locks @lock for writing. If @lock is already locked for writing or
1129 reading by other threads, this function will block until @lock is
1130 completely unlocked and then lock @lock for writing. While this
1131 functions waits to lock @lock, no other thread can lock @lock for
1132 reading. When @lock is locked for writing, no other thread can lock
1133 @lock (neither for reading nor writing). This lock has to be unlocked
1134 by g_static_rw_lock_writer_unlock().
1137 @lock: a #GStaticRWLock to lock for writing
1140 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1142 Tries to lock @lock for writing. If @lock is already locked (for
1143 either reading or writing) by another thread, it immediately returns
1144 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1145 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1148 @lock: a #GStaticRWLock to lock for writing
1149 @Returns: TRUE, if @lock could be locked for writing
1152 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1154 Unlocks @lock. If a thread waits to lock @lock for writing and all
1155 locks for reading have been unlocked, the waiting thread is woken up
1156 and can lock @lock for writing. If no thread waits to lock @lock for
1157 writing and threads wait to lock @lock for reading, the waiting
1158 threads are woken up and can lock @lock for reading.
1161 @lock: a #GStaticRWLock to unlock after writing
1164 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1166 Releases all resources allocated to @lock.
1170 You don't have to call this functions for a #GStaticRWLock with an
1171 unbounded lifetime, i.e. objects declared 'static', but if you have a
1172 #GStaticRWLock as a member of a structure and the structure is freed,
1173 you should also free the #GStaticRWLock.
1176 @lock: a #GStaticRWLock to be freed
1179 <!-- ##### STRUCT GCond ##### -->
1182 The #GCond struct is an opaque data structure to represent a
1183 condition. A #GCond is an object, that threads can block on, if they
1184 find a certain condition to be false. If other threads change the
1185 state of this condition they can signal the #GCond, such that the
1186 waiting thread is woken up.
1191 <title>Using GCond to block a thread until a condition is satisfied</title>
1193 GCond* data_cond = NULL; /* Must be initialized somewhere */
1194 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1195 gpointer current_data = NULL;
1197 void push_data (gpointer data)
1199 g_mutex_lock (data_mutex);
1200 current_data = data;
1201 g_cond_signal (data_cond);
1202 g_mutex_unlock (data_mutex);
1205 gpointer pop_data ()
1209 g_mutex_lock (data_mutex);
1210 while (!current_data)
1211 g_cond_wait (data_cond, data_mutex);
1212 data = current_data;
1213 current_data = NULL;
1214 g_mutex_unlock (data_mutex);
1222 Whenever a thread calls pop_data() now, it will wait until
1223 current_data is non-NULL, i.e. until some other thread has called
1229 It is important to use the g_cond_wait() and g_cond_timed_wait()
1230 functions only inside a loop, which checks for the condition to be
1231 true as it is not guaranteed that the waiting thread will find it
1232 fulfilled, even if the signaling thread left the condition
1233 in that state. This is because another thread can have altered the
1234 condition, before the waiting thread got the chance to be woken up,
1235 even if the condition itself is protected by a #GMutex, like above.
1240 A #GCond should only be accessed via the following functions.
1245 All of the g_cond_* functions are actually macros. Apart from taking
1246 the addresses of them, you can however use them as if they were functions.
1251 <!-- ##### FUNCTION g_cond_new ##### -->
1254 Creates a new #GCond. This function will abort, if g_thread_init()
1255 has not been called yet.
1258 @Returns: a new #GCond
1261 <!-- ##### FUNCTION g_cond_signal ##### -->
1263 If threads are waiting for @cond, exactly one of them is woken up. It
1264 is good practice to hold the same lock as the waiting thread, while
1265 calling this function, though not required.
1269 This function can also be used, if g_thread_init() has
1270 not yet been called and will do nothing then.
1276 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1279 If threads are waiting for @cond, all of them are woken up. It is good
1280 practice to lock the same mutex as the waiting threads, while calling
1281 this function, though not required.
1285 This function can also be used, if g_thread_init() has
1286 not yet been called and will do nothing then.
1292 <!-- ##### FUNCTION g_cond_wait ##### -->
1295 Waits until this thread is woken up on @cond. The @mutex is unlocked
1296 before falling asleep and locked again before resuming.
1300 This function can also be used, if g_thread_init() has not yet been
1301 called and will immediately return then.
1305 @mutex: a #GMutex, that is currently locked
1308 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1311 Waits until this thread is woken up on @cond, but not longer than
1312 until the time, that is specified by @abs_time. The @mutex is
1313 unlocked before falling asleep and locked again before resuming.
1317 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1321 This function can also be used, if g_thread_init() has not yet been
1322 called and will immediately return TRUE then.
1326 @mutex: a #GMutex, that is currently locked
1327 @abs_time: a #GTimeVal, determining the final time
1328 @Returns: TRUE, if the thread is woken up in time
1331 <!-- ##### FUNCTION g_cond_free ##### -->
1334 Destroys the #GCond.
1340 <!-- ##### STRUCT GPrivate ##### -->
1342 The #GPrivate struct is an opaque data structure to represent a thread
1343 private data key. Threads can thereby obtain and set a pointer, which
1344 is private to the current thread. Take our give_me_next_number()
1345 example from above. Now we don't want current_number to be shared
1346 between the threads, but to be private to each thread. This can be
1350 <title>Using GPrivate for per-thread data</title>
1352 GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1353 /* with g_private_new (g_free); */
1355 int give_me_next_number ()
1357 int *current_number = g_private_get (current_number_key);
1359 if (!current_number)
1361 current_number = g_new (int,1);
1362 *current_number = 0;
1363 g_private_set (current_number_key, current_number);
1365 *current_number = calc_next_number (*current_number);
1366 return *current_number;
1373 Here the pointer belonging to the key current_number_key is read. If
1374 it is NULL, it has not been set yet. Then get memory for an integer
1375 value, assign this memory to the pointer and write the pointer
1376 back. Now we have an integer value, that is private to the current
1381 The #GPrivate struct should only be accessed via the following functions.
1386 All of the g_private_* functions are actually macros. Apart from taking
1387 the addresses of them, you can however use them as if they were functions.
1392 <!-- ##### FUNCTION g_private_new ##### -->
1395 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1396 to a destructor function. Whenever a thread ends and the corresponding
1397 pointer keyed to this instance of #GPrivate is non-NULL, the
1398 destructor is called with this pointer as the argument.
1403 @destructor is working quite differently from @notify in
1404 g_static_private_set().
1410 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1411 shortage or use #GStaticPrivate.
1417 This function will abort, if g_thread_init() has not been called yet.
1421 @destructor: a function to handle the data keyed to #GPrivate, when a
1423 @Returns: a new #GPrivate
1426 <!-- ##### FUNCTION g_private_get ##### -->
1429 Returns the pointer keyed to @private_key for the current thread. This
1430 pointer is NULL, when g_private_set() hasn't been called for the
1431 current @private_key and thread yet.
1435 This function can also be used, if g_thread_init() has not yet been
1436 called and will return the value of @private_key casted to #gpointer then.
1439 @private_key: a #GPrivate
1440 @Returns: the corresponding pointer
1443 <!-- ##### FUNCTION g_private_set ##### -->
1446 Sets the pointer keyed to @private_key for the current thread.
1450 This function can also be used, if g_thread_init() has not yet been
1451 called and will set @private_key to @data casted to #GPrivate* then.
1454 @private_key: a #GPrivate
1455 @data: the new pointer
1458 <!-- ##### STRUCT GStaticPrivate ##### -->
1461 A #GStaticPrivate works almost like a #GPrivate, but it has one
1462 significant advantage. It doesn't need to be created at run-time like
1463 a #GPrivate, but can be defined at compile-time. This is similar to
1464 the difference between #GMutex and #GStaticMutex. Now look at our
1465 give_me_next_number() example with #GStaticPrivate:
1470 <title>Using GStaticPrivate for per-thread data</title>
1472 int give_me_next_number ()
1474 static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1475 int *current_number = g_static_private_get (&current_number_key);
1477 if (!current_number)
1479 current_number = g_new (int,1);
1480 *current_number = 0;
1481 g_static_private_set (&current_number_key, current_number, g_free);
1483 *current_number = calc_next_number (*current_number);
1484 return *current_number;
1492 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1494 Every #GStaticPrivate must be initialized with this macro, before it can
1501 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1508 <!-- ##### FUNCTION g_static_private_init ##### -->
1510 Initializes @private_key. Alternatively you can initialize it with
1511 #G_STATIC_PRIVATE_INIT.
1514 @private_key: a #GStaticPrivate to be initialized
1517 <!-- ##### FUNCTION g_static_private_get ##### -->
1519 Works like g_private_get() only for a #GStaticPrivate.
1523 This function also works, if g_thread_init() has not yet been called.
1526 @private_key: a #GStaticPrivate
1527 @Returns: the corresponding pointer
1530 <!-- ##### FUNCTION g_static_private_set ##### -->
1532 Sets the pointer keyed to @private_key for the current thread and the
1533 function @notify to be called with that pointer (NULL or non-NULL),
1534 whenever the pointer is set again or whenever the current thread ends.
1538 This function also works, if g_thread_init() has not yet been
1539 called. If g_thread_init() is called later, the @data keyed to
1540 @private_key will be inherited only by the main thread, i.e. the one that
1541 called g_thread_init().
1546 @notify is working quite differently from @destructor in
1551 @private_key: a #GStaticPrivate
1552 @data: the new pointer
1553 @notify: a function to be called with the pointer, whenever the
1554 current thread ends or sets this pointer again
1557 <!-- ##### FUNCTION g_static_private_free ##### -->
1559 Releases all resources allocated to @private_key.
1563 You don't have to call this functions for a #GStaticPrivate with an
1564 unbounded lifetime, i.e. objects declared 'static', but if you have a
1565 #GStaticPrivate as a member of a structure and the structure is freed,
1566 you should also free the #GStaticPrivate.
1569 @private_key: a #GStaticPrivate to be freed