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:
142 <!-- ##### FUNCTION g_thread_init ##### -->
145 Before you use a thread related function in GLib, you should
146 initialize the thread system. This is done by calling
147 g_thread_init(). Most of the time you will only have to call
148 <literal>g_thread_init(NULL)</literal>.
153 You should only call g_thread_init() with a non-%NULL parameter if you
154 really know what you are doing.
160 g_thread_init() must not be called directly or indirectly as a
161 callback from GLib. Also no mutexes may be currently locked, while
162 calling g_thread_init().
167 g_thread_init() might only be called once. On the second call
168 it will abort with an error. If you want to make sure, that the thread
169 system is initialized, you can do that too:
175 if (!g_thread_supported (<!-- -->)) g_thread_init (NULL);
181 After that line either the thread system is initialized or the program
182 will abort, if no thread system is available in GLib, i.e. either
183 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
187 If no thread system is available and @vtable is %NULL or if not all
188 elements of @vtable are non-%NULL, then g_thread_init() will abort.
193 To use g_thread_init() in your program, you have to link with the
194 libraries that the command <command>pkg-config --libs gthread-2.0</command>
195 outputs. This is not the case for all the other thread related functions of
196 GLib. Those can be used without having to link with the thread libraries.
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 @func functions passed to
223 g_thread_create() or g_thread_create_full().
226 @data: data passed 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.
266 <!-- ##### FUNCTION g_thread_create ##### -->
268 This function creates a new thread with the default priority.
272 If @joinable is %TRUE, you can wait for this threads termination
273 calling g_thread_join(). Otherwise the thread will just disappear, when
278 The new thread executes the function @func with the argument
279 @data. If the thread was created successfully, it is returned.
283 @error can be %NULL to ignore errors, or non-%NULL to report errors. The
284 error is set, if and only if the function returns %NULL.
287 @func: a function to execute in the new thread.
288 @data: an argument to supply to the new thread.
289 @joinable: should this thread be joinable?
290 @error: return location for error.
291 @Returns: the new #GThread on success.
294 <!-- ##### FUNCTION g_thread_create_full ##### -->
296 This function creates a new thread with the priority @priority. The
297 stack gets the size @stack_size or the default value for the current
298 platform, if @stack_size is 0.
302 If @joinable is %TRUE, you can wait for this threads termination
303 calling g_thread_join(). Otherwise the thread will just disappear, when
304 ready. If @bound is %TRUE, this thread will be scheduled in the system
305 scope, otherwise the implementation is free to do scheduling in the
306 process scope. The first variant is more expensive resource-wise, but
307 generally faster. On some systems (e.g. Linux) all threads are bound.
311 The new thread executes the function @func with the argument
312 @data. If the thread was created successfully, it is returned.
316 @error can be %NULL to ignore errors, or non-%NULL to report errors. The
317 error is set, if and only if the function returns %NULL.
322 It is not guaranteed, that threads with different priorities really
323 behave accordingly. On some systems (e.g. Linux) only root can increase
324 priorities. On other systems (e.g. Solaris) there doesn't seem to be
325 different scheduling for different priorities. All in all try to avoid
326 being dependent on priorities. Use %G_THREAD_PRIORITY_NORMAL here as a
333 Only use g_thread_create_full(), when you really can't use
334 g_thread_create() instead. g_thread_create() does not take
335 @stack_size, @bound and @priority as arguments, as they should only be
336 used for cases, where it is inevitable.
340 @func: a function to execute in the new thread.
341 @data: an argument to supply to the new thread.
342 @stack_size: a stack size for the new thread.
343 @joinable: should this thread be joinable?
344 @bound: should this thread be bound to a system thread?
345 @priority: a priority for the thread.
346 @error: return location for error.
347 @Returns: the new #GThread on success.
350 <!-- ##### FUNCTION g_thread_self ##### -->
352 This functions returns the #GThread corresponding to the calling thread.
355 @Returns: the current thread.
358 <!-- ##### FUNCTION g_thread_join ##### -->
360 Waits until @thread finishes, i.e. the function @func, as given
361 to g_thread_create(), returns or g_thread_exit() is called by
362 @thread. All resources of @thread including the #GThread struct are
363 released. @thread must have been created with @joinable=%TRUE in
364 g_thread_create(). The value returned by @func or given to
365 g_thread_exit() by @thread is returned by this function.
368 @thread: a #GThread to be waited for.
369 @Returns: the return value of the thread.
372 <!-- ##### FUNCTION g_thread_set_priority ##### -->
374 Changes the priority of @thread to @priority.
379 It is not guaranteed, that threads with different priorities really
380 behave accordingly. On some systems (e.g. Linux) only root can increase
381 priorities. On other systems (e.g. Solaris) there doesn't seem to be
382 different scheduling for different priorities. All in all try to avoid
383 being dependent on priorities.
388 @priority: a new priority for @thread.
391 <!-- ##### FUNCTION g_thread_yield ##### -->
393 Gives way to other threads waiting to be scheduled.
397 This function is often used as a method to make busy wait less
398 evil. But in most cases, you will encounter, there are better methods
399 to do that. So in general you shouldn't use that function.
404 <!-- ##### FUNCTION g_thread_exit ##### -->
406 Exits the current thread. If another thread is waiting for that thread
407 using g_thread_join() and the current thread is joinable, the waiting
408 thread will be woken up and getting @retval as the return value of
409 g_thread_join(). If the current thread is not joinable, @retval is
416 g_thread_exit (retval);
422 is equivalent to calling
434 in the function @func, as given to g_thread_create().
439 Never call g_thread_exit() from within a thread of a #GThreadPool, as
440 that will mess up the bookkeeping and lead to funny and unwanted results.
444 @retval: the return value of this thread.
447 <!-- ##### STRUCT GMutex ##### -->
450 The #GMutex struct is an opaque data structure to represent a mutex
451 (mutual exclusion). It can be used to protect data against shared
452 access. Take for example the following function:
455 <title>A function which will not work in a threaded environment</title>
457 int give_me_next_number (<!-- -->)
459 static int current_number = 0;
461 /* now do a very complicated calculation to calculate the new number,
462 this might for example be a random number generator */
463 current_number = calc_next_number (current_number);
464 return current_number;
471 It is easy to see, that this won't work in a multi-threaded
472 application. There current_number must be protected against shared
473 access. A first naive implementation would be:
478 <title>The wrong way to write a thread-safe function</title>
480 int give_me_next_number (<!-- -->)
482 static int current_number = 0;
484 static GMutex * mutex = NULL;
487 mutex = g_mutex_new (<!-- -->);
488 g_mutex_lock (mutex);
489 ret_val = current_number = calc_next_number (current_number);
490 g_mutex_unlock (mutex);
498 This looks like it would work, but there is a race condition while
499 constructing the mutex and this code cannot work reliable. So please do
500 not use such constructs in your own programs. One working solution is:
505 <title>A correct thread-safe function</title>
507 static GMutex *give_me_next_number_mutex = NULL;
509 /* this function must be called before any call to give_me_next_number (<!-- -->)
510 it must be called exactly once. */
511 void init_give_me_next_number (<!-- -->)
513 g_assert (give_me_next_number_mutex == NULL);
514 give_me_next_number_mutex = g_mutex_new (<!-- -->);
517 int give_me_next_number (<!-- -->)
519 static int current_number = 0;
522 g_mutex_lock (give_me_next_number_mutex);
523 ret_val = current_number = calc_next_number (current_number);
524 g_mutex_unlock (give_me_next_number_mutex);
532 #GStaticMutex provides a simpler and safer way of doing this.
536 If you want to use a mutex, but your code should also work without
537 calling g_thread_init() first, you can not use a #GMutex, as
538 g_mutex_new() requires that. Use a #GStaticMutex instead.
542 A #GMutex should only be accessed via the following functions.
547 All of the <function>g_mutex_*</function> functions are actually macros.
548 Apart from taking their addresses, you can however use them as if they
554 <!-- ##### FUNCTION g_mutex_new ##### -->
557 Creates a new #GMutex.
562 This function will abort, if g_thread_init() has not been called yet.
566 @Returns: a new #GMutex.
569 <!-- ##### FUNCTION g_mutex_lock ##### -->
572 Locks @mutex. If @mutex is already locked by another thread, the
573 current thread will block until @mutex is unlocked by the other
578 This function can also be used, if g_thread_init() has not yet been
579 called and will do nothing then.
584 #GMutex is neither guaranteed to be recursive nor to be non-recursive,
585 i.e. a thread could deadlock while calling g_mutex_lock(), if it
586 already has locked @mutex. Use #GStaticRecMutex, if you need recursive
594 <!-- ##### FUNCTION g_mutex_trylock ##### -->
597 Tries to lock @mutex. If @mutex is already locked by another
598 thread, it immediately returns %FALSE. Otherwise it locks @mutex
603 This function can also be used, if g_thread_init() has not yet been
604 called and will immediately return %TRUE then.
609 #GMutex is neither guaranteed to be recursive nor to be non-recursive,
610 i.e. the return value of g_mutex_trylock() could be both %FALSE or
611 %TRUE, if the current thread already has locked @mutex. Use
612 #GStaticRecMutex, if you need recursive mutexes.
617 @Returns: %TRUE, if @mutex could be locked.
620 <!-- ##### FUNCTION g_mutex_unlock ##### -->
623 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
624 for @mutex, it will be woken and can lock @mutex itself.
628 This function can also be used, if g_thread_init() has not yet been
629 called and will do nothing then.
635 <!-- ##### FUNCTION g_mutex_free ##### -->
644 <!-- ##### STRUCT GStaticMutex ##### -->
647 A #GStaticMutex works like a #GMutex, but it has one significant
648 advantage. It doesn't need to be created at run-time like a #GMutex,
649 but can be defined at compile-time. Here is a shorter, easier and
650 safer version of our <function>give_me_next_number()</function> example:
655 <title>Using <structname>GStaticMutex</structname> to simplify thread-safe programming</title>
657 int give_me_next_number (<!-- -->)
659 static int current_number = 0;
661 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
663 g_static_mutex_lock (&mutex);
664 ret_val = current_number = calc_next_number (current_number);
665 g_static_mutex_unlock (&mutex);
673 Sometimes you would like to dynamically create a mutex. If you don't
674 want to require prior calling to g_thread_init(), because your code
675 should also be usable in non-threaded programs, you are not able to
676 use g_mutex_new() and thus #GMutex, as that requires a prior call to
677 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
678 must be initialized with g_static_mutex_init() before using it and
679 freed with with g_static_mutex_free() when not needed anymore to free
680 up any allocated resources.
684 Even though #GStaticMutex is not opaque, it should only be used with
685 the following functions, as it is defined differently on different
690 All of the <function>g_static_mutex_*</function> functions can also be
691 used, if g_thread_init() has not yet been called.
696 All of the <function>g_static_mutex_*</function> functions are actually
697 macros. Apart from taking their addresses, you can however use them
698 as if they were functions.
703 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
706 A #GStaticMutex must be initialized with this macro, before it can be
707 used. This macro can used be to initialize a variable, but it cannot
708 be assigned to a variable. In that case you have to use
709 g_static_mutex_init().
715 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
722 <!-- ##### FUNCTION g_static_mutex_init ##### -->
724 Initializes @mutex. Alternatively you can initialize it with
725 #G_STATIC_MUTEX_INIT.
728 @mutex: a #GStaticMutex to be initialized.
731 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
733 Works like g_mutex_lock(), but for a #GStaticMutex.
736 @mutex: a #GStaticMutex.
739 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
742 Works like g_mutex_trylock(), but for a #GStaticMutex.
745 @mutex: a #GStaticMutex.
746 @Returns: %TRUE, if the #GStaticMutex could be locked.
749 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
752 Works like g_mutex_unlock(), but for a #GStaticMutex.
755 @mutex: a #GStaticMutex.
758 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
761 For some operations (like g_cond_wait()) you must have a #GMutex
762 instead of a #GStaticMutex. This function will return the
763 corresponding #GMutex for @mutex.
766 @mutex: a #GStaticMutex.
767 @Returns: the #GMutex corresponding to @mutex.
770 <!-- ##### FUNCTION g_static_mutex_free ##### -->
772 Releases all resources allocated to @mutex.
776 You don't have to call this functions for a #GStaticMutex with an
777 unbounded lifetime, i.e. objects declared 'static', but if you have a
778 #GStaticMutex as a member of a structure and the structure is freed,
779 you should also free the #GStaticMutex.
782 @mutex: a #GStaticMutex to be freed.
785 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
788 The %G_LOCK_* macros provide a convenient interface to #GStaticMutex
789 with the advantage that they will expand to nothing in programs
790 compiled against a thread-disabled GLib, saving code and memory
791 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
792 definitions may appear in programs, i.e. in the first block of a
793 function or outside of functions. The @name parameter will be mangled
794 to get the name of the #GStaticMutex. This means, that you can use
795 names of existing variables as the parameter, e.g. the name of the
796 variable you intent to protect with the lock. Look at our
797 <function>give_me_next_number()</function> example using the %G_LOCK_* macros:
802 <title>Using the %G_LOCK_* convenience macros</title>
804 G_LOCK_DEFINE (current_number);
806 int give_me_next_number (<!-- -->)
808 static int current_number = 0;
811 G_LOCK (current_number);
812 ret_val = current_number = calc_next_number (current_number);
813 G_UNLOCK (current_number);
820 @name: the name of the lock.
823 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
826 This works like #G_LOCK_DEFINE, but it creates a static object.
829 @name: the name of the lock.
832 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
835 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
838 @name: the name of the lock.
841 <!-- ##### MACRO G_LOCK ##### -->
844 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
847 @name: the name of the lock.
850 <!-- ##### MACRO G_TRYLOCK ##### -->
853 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
856 @name: the name of the lock.
857 @Returns: %TRUE, if the lock could be locked.
860 <!-- ##### MACRO G_UNLOCK ##### -->
863 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
866 @name: the name of the lock.
869 <!-- ##### STRUCT GStaticRecMutex ##### -->
871 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
872 multiple times by one thread. If you enter it n times, however, you
873 have to unlock it n times again to let other threads lock it. An
874 exception is the function g_static_rec_mutex_unlock_full(), that
875 allows you to unlock a #GStaticRecMutex completely returning the depth,
876 i.e. the number of times this mutex was locked. The depth can later be
877 used to restore the state by calling g_static_rec_mutex_lock_full().
881 Even though #GStaticRecMutex is not opaque, it should only be used with
882 the following functions.
886 All of the <function>g_static_rec_mutex_*</function> functions can also
887 be used, if g_thread_init() has not been called.
891 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
893 A #GStaticRecMutex must be initialized with this macro, before it can
894 be used. This macro can used be to initialize a variable, but it
895 cannot be assigned to a variable. In that case you have to use
896 g_static_rec_mutex_init().
902 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
909 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
911 A #GStaticRecMutex must be initialized with this function, before it
912 can be used. Alternatively you can initialize it with
913 #G_STATIC_REC_MUTEX_INIT.
916 @mutex: a #GStaticRecMutex to be initialized.
919 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
921 Locks @mutex. If @mutex is already locked by another thread, the
922 current thread will block until @mutex is unlocked by the other
923 thread. If @mutex is already locked by the calling thread, this
924 functions increases the depth of @mutex and returns immediately.
927 @mutex: a #GStaticRecMutex to lock.
930 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
932 Tries to lock @mutex. If @mutex is already locked by another thread,
933 it immediately returns %FALSE. Otherwise it locks @mutex and returns
934 %TRUE. If @mutex is already locked by the calling thread, this
935 functions increases the depth of @mutex and immediately returns %TRUE.
938 @mutex: a #GStaticRecMutex to lock.
939 @Returns: %TRUE, if @mutex could be locked.
942 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
944 Unlocks @mutex. Another threads can, however, only lock @mutex when it
945 has been unlocked as many times, as it had been locked before. If
946 @mutex is completely unlocked and another thread is blocked in a
947 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
951 @mutex: a #GStaticRecMutex to unlock.
954 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
956 Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
959 @mutex: a #GStaticRecMutex to lock.
960 @depth: number of times this mutex has to be unlocked to be completely unlocked.
963 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
965 Completely unlocks @mutex. If another thread is blocked in a
966 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
967 lock @mutex itself. This function returns the number of times, that
968 @mutex has been locked by the current thread. To restore the state
969 before the call to g_static_rec_mutex_unlock_full() you can call
970 g_static_rec_mutex_lock_full() with the depth returned by this
974 @mutex: a #GStaticRecMutex to completely unlock.
975 @Returns: number of times @mutex has been locked by the current thread.
978 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
980 Releases all resources allocated to a #GStaticRecMutex.
984 You don't have to call this functions for a #GStaticRecMutex with an
985 unbounded lifetime, i.e. objects declared 'static', but if you have a
986 #GStaticRecMutex as a member of a structure and the structure is
987 freed, you should also free the #GStaticRecMutex.
990 @mutex: a #GStaticRecMutex to be freed.
993 <!-- ##### STRUCT GStaticRWLock ##### -->
995 The #GStaticRWLock struct represents a read-write lock. A read-write
996 lock can be used for protecting data, that some portions of code only
997 read from, while others also write. In such situations it is
998 desirable, that several readers can read at once, whereas of course
999 only one writer may write at a time. Take a look at the following
1003 <title>An array with access functions</title>
1005 GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
1009 gpointer my_array_get (guint index)
1011 gpointer retval = NULL;
1016 g_static_rw_lock_reader_lock (&rwlock);
1018 if (index < array->len)
1019 retval = g_ptr_array_index (array, index);
1021 g_static_rw_lock_reader_unlock (&rwlock);
1026 void my_array_set (guint index, gpointer data)
1028 g_static_rw_lock_writer_lock (&rwlock);
1031 array = g_ptr_array_new (<!-- -->);
1033 if (index >= array->len)
1034 g_ptr_array_set_size (array, index+1);
1036 g_ptr_array_index (array, index) = data;
1038 g_static_rw_lock_writer_unlock (&rwlock);
1045 This example shows an array, which can be accessed by many readers
1046 (the <function>my_array_get()</function> function) simultaneously,
1047 whereas the writers (the <function>my_array_set()</function> function)
1048 will only be allowed once a time and only if no readers currently access
1049 the array. This is because of the potentially dangerous resizing of the
1050 array. Using these functions is fully multi-thread safe now.
1054 Most of the time the writers should have precedence of readers. That
1055 means for this implementation, that as soon as a writer wants to lock
1056 the data, no other reader is allowed to lock the data, whereas of
1057 course the readers, that already have locked the data are allowed to
1058 finish their operation. As soon as the last reader unlocks the data,
1059 the writer will lock it.
1063 Even though #GStaticRWLock is not opaque, it should only be used with
1064 the following functions.
1068 All of the <function>g_static_rw_lock_*</function> functions can also be
1069 used, if g_thread_init() has not been called.
1074 A read-write lock has a higher overhead as a mutex. For example both
1075 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1076 have to lock and unlock a #GStaticMutex, so it takes at least twice the
1077 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1078 #GStaticMutex. So only data structures, that are accessed by multiple
1079 readers, which keep the lock for a considerable time justify a
1080 #GStaticRWLock. The above example most probably would fare better with
1086 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1088 A #GStaticRWLock must be initialized with this macro, before it can
1089 be used. This macro can used be to initialize a variable, but it
1090 cannot be assigned to a variable. In that case you have to use
1091 g_static_rw_lock_init().
1097 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1104 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1106 A #GStaticRWLock must be initialized with this function, before it can
1107 be used. Alternatively you can initialize it with
1108 #G_STATIC_RW_LOCK_INIT.
1111 @lock: a #GStaticRWLock to be initialized.
1114 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1116 Locks @lock for reading. There may be unlimited concurrent locks for
1117 reading of a #GStaticRWLock at the same time. If @lock is already
1118 locked for writing by another thread or if another thread is already
1119 waiting to lock @lock for writing, this function will block until
1120 @lock is unlocked by the other writing thread and no other writing
1121 threads want to lock @lock. This lock has to be unlocked by
1122 g_static_rw_lock_reader_unlock().
1126 #GStaticRWLock is not recursive. It might seem to be possible to
1127 recursively lock for reading, but that can result in a deadlock as
1128 well, due to writer preference.
1131 @lock: a #GStaticRWLock to lock for reading.
1134 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1136 Tries to lock @lock for reading. If @lock is already locked for
1137 writing by another thread or if another thread is already waiting to
1138 lock @lock for writing, it immediately returns %FALSE. Otherwise it
1139 locks @lock for reading and returns %TRUE. This lock has to be unlocked
1140 by g_static_rw_lock_reader_unlock().
1143 @lock: a #GStaticRWLock to lock for reading.
1144 @Returns: %TRUE, if @lock could be locked for reading.
1147 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1149 Unlocks @lock. If a thread waits to lock @lock for writing and all
1150 locks for reading have been unlocked, the waiting thread is woken up
1151 and can lock @lock for writing.
1154 @lock: a #GStaticRWLock to unlock after reading.
1157 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1159 Locks @lock for writing. If @lock is already locked for writing or
1160 reading by other threads, this function will block until @lock is
1161 completely unlocked and then lock @lock for writing. While this
1162 functions waits to lock @lock, no other thread can lock @lock for
1163 reading. When @lock is locked for writing, no other thread can lock
1164 @lock (neither for reading nor writing). This lock has to be unlocked
1165 by g_static_rw_lock_writer_unlock().
1168 @lock: a #GStaticRWLock to lock for writing.
1171 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1173 Tries to lock @lock for writing. If @lock is already locked (for
1174 either reading or writing) by another thread, it immediately returns
1175 %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1176 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1179 @lock: a #GStaticRWLock to lock for writing.
1180 @Returns: %TRUE, if @lock could be locked for writing.
1183 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1185 Unlocks @lock. If a thread waits to lock @lock for writing and all
1186 locks for reading have been unlocked, the waiting thread is woken up
1187 and can lock @lock for writing. If no thread waits to lock @lock for
1188 writing and threads wait to lock @lock for reading, the waiting
1189 threads are woken up and can lock @lock for reading.
1192 @lock: a #GStaticRWLock to unlock after writing.
1195 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1197 Releases all resources allocated to @lock.
1201 You don't have to call this functions for a #GStaticRWLock with an
1202 unbounded lifetime, i.e. objects declared 'static', but if you have a
1203 #GStaticRWLock as a member of a structure and the structure is freed,
1204 you should also free the #GStaticRWLock.
1207 @lock: a #GStaticRWLock to be freed.
1210 <!-- ##### STRUCT GCond ##### -->
1213 The #GCond struct is an opaque data structure to represent a
1214 condition. A #GCond is an object, that threads can block on, if they
1215 find a certain condition to be false. If other threads change the
1216 state of this condition they can signal the #GCond, such that the
1217 waiting thread is woken up.
1222 <title>Using GCond to block a thread until a condition is satisfied</title>
1224 GCond* data_cond = NULL; /* Must be initialized somewhere */
1225 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1226 gpointer current_data = NULL;
1228 void push_data (gpointer data)
1230 g_mutex_lock (data_mutex);
1231 current_data = data;
1232 g_cond_signal (data_cond);
1233 g_mutex_unlock (data_mutex);
1236 gpointer pop_data (<!-- -->)
1240 g_mutex_lock (data_mutex);
1241 while (!current_data)
1242 g_cond_wait (data_cond, data_mutex);
1243 data = current_data;
1244 current_data = NULL;
1245 g_mutex_unlock (data_mutex);
1253 Whenever a thread calls <function>pop_data()</function> now, it will
1254 wait until current_data is non-%NULL, i.e. until some other thread
1255 has called <function>push_data()</function>.
1260 It is important to use the g_cond_wait() and g_cond_timed_wait()
1261 functions only inside a loop, which checks for the condition to be
1262 true as it is not guaranteed that the waiting thread will find it
1263 fulfilled, even if the signaling thread left the condition
1264 in that state. This is because another thread can have altered the
1265 condition, before the waiting thread got the chance to be woken up,
1266 even if the condition itself is protected by a #GMutex, like above.
1271 A #GCond should only be accessed via the following functions.
1276 All of the <function>g_cond_*</function> functions are actually macros.
1277 Apart from taking their addresses, you can however use them as if they
1283 <!-- ##### FUNCTION g_cond_new ##### -->
1286 Creates a new #GCond. This function will abort, if g_thread_init()
1287 has not been called yet.
1290 @Returns: a new #GCond.
1293 <!-- ##### FUNCTION g_cond_signal ##### -->
1295 If threads are waiting for @cond, exactly one of them is woken up. It
1296 is good practice to hold the same lock as the waiting thread, while
1297 calling this function, though not required.
1301 This function can also be used, if g_thread_init() has
1302 not yet been called and will do nothing then.
1308 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1311 If threads are waiting for @cond, all of them are woken up. It is good
1312 practice to lock the same mutex as the waiting threads, while calling
1313 this function, though not required.
1317 This function can also be used, if g_thread_init() has
1318 not yet been called and will do nothing then.
1324 <!-- ##### FUNCTION g_cond_wait ##### -->
1327 Waits until this thread is woken up on @cond. The @mutex is unlocked
1328 before falling asleep and locked again before resuming.
1332 This function can also be used, if g_thread_init() has not yet been
1333 called and will immediately return then.
1337 @mutex: a #GMutex, that is currently locked.
1340 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1343 Waits until this thread is woken up on @cond, but not longer than
1344 until the time, that is specified by @abs_time. The @mutex is
1345 unlocked before falling asleep and locked again before resuming.
1349 If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
1353 This function can also be used, if g_thread_init() has not yet been
1354 called and will immediately return %TRUE then.
1358 To easily calculate @abs_time a combination of g_get_current_time()
1359 and g_time_val_add() can be used.
1363 @mutex: a #GMutex, that is currently locked.
1364 @abs_time: a #GTimeVal, determining the final time.
1365 @Returns: %TRUE, if the thread is woken up in time.
1368 <!-- ##### FUNCTION g_cond_free ##### -->
1371 Destroys the #GCond.
1377 <!-- ##### STRUCT GPrivate ##### -->
1379 The #GPrivate struct is an opaque data structure to represent a thread
1380 private data key. Threads can thereby obtain and set a pointer, which
1381 is private to the current thread.
1382 Take our <function>give_me_next_number()</function> example from above.
1383 Now we don't want <literal>current_number</literal> to be shared
1384 between the threads, but to be private to each thread. This can be
1388 <title>Using GPrivate for per-thread data</title>
1390 GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1391 /* with g_private_new (g_free); */
1393 int give_me_next_number (<!-- -->)
1395 int *current_number = g_private_get (current_number_key);
1397 if (!current_number)
1399 current_number = g_new (int,1);
1400 *current_number = 0;
1401 g_private_set (current_number_key, current_number);
1403 *current_number = calc_next_number (*current_number);
1404 return *current_number;
1411 Here the pointer belonging to the key <literal>current_number_key</literal>
1412 is read. If it is %NULL, it has not been set yet. Then get memory for an
1413 integer value, assign this memory to the pointer and write the pointer
1414 back. Now we have an integer value, that is private to the current thread.
1418 The #GPrivate struct should only be accessed via the following functions.
1423 All of the <function>g_private_*</function> functions are actually macros.
1424 Apart from taking their addresses, you can however use them as if they were
1430 <!-- ##### FUNCTION g_private_new ##### -->
1433 Creates a new #GPrivate. If @destructor is non-%NULL, it is a pointer
1434 to a destructor function. Whenever a thread ends and the corresponding
1435 pointer keyed to this instance of #GPrivate is non-%NULL, the
1436 destructor is called with this pointer as the argument.
1441 @destructor is working quite differently from @notify in
1442 g_static_private_set().
1448 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1449 shortage or use #GStaticPrivate.
1455 This function will abort, if g_thread_init() has not been called yet.
1459 @destructor: a function to handle the data keyed to #GPrivate, when a
1461 @Returns: a new #GPrivate.
1464 <!-- ##### FUNCTION g_private_get ##### -->
1467 Returns the pointer keyed to @private_key for the current thread. This
1468 pointer is %NULL, when g_private_set() hasn't been called for the
1469 current @private_key and thread yet.
1473 This function can also be used, if g_thread_init() has not yet been
1474 called and will return the value of @private_key casted to #gpointer then.
1477 @private_key: a #GPrivate.
1478 @Returns: the corresponding pointer.
1481 <!-- ##### FUNCTION g_private_set ##### -->
1484 Sets the pointer keyed to @private_key for the current thread.
1488 This function can also be used, if g_thread_init() has not yet been
1489 called and will set @private_key to @data casted to #GPrivate* then.
1492 @private_key: a #GPrivate.
1493 @data: the new pointer.
1496 <!-- ##### STRUCT GStaticPrivate ##### -->
1499 A #GStaticPrivate works almost like a #GPrivate, but it has one
1500 significant advantage. It doesn't need to be created at run-time like
1501 a #GPrivate, but can be defined at compile-time. This is similar to
1502 the difference between #GMutex and #GStaticMutex. Now look at our
1503 <function>give_me_next_number()</function> example with #GStaticPrivate:
1508 <title>Using GStaticPrivate for per-thread data</title>
1510 int give_me_next_number (<!-- -->)
1512 static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1513 int *current_number = g_static_private_get (&current_number_key);
1515 if (!current_number)
1517 current_number = g_new (int,1);
1518 *current_number = 0;
1519 g_static_private_set (&current_number_key, current_number, g_free);
1521 *current_number = calc_next_number (*current_number);
1522 return *current_number;
1529 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1531 Every #GStaticPrivate must be initialized with this macro, before it can
1538 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1545 <!-- ##### FUNCTION g_static_private_init ##### -->
1547 Initializes @private_key. Alternatively you can initialize it with
1548 #G_STATIC_PRIVATE_INIT.
1551 @private_key: a #GStaticPrivate to be initialized.
1554 <!-- ##### FUNCTION g_static_private_get ##### -->
1556 Works like g_private_get() only for a #GStaticPrivate.
1560 This function also works, if g_thread_init() has not yet been called.
1563 @private_key: a #GStaticPrivate.
1564 @Returns: the corresponding pointer.
1567 <!-- ##### FUNCTION g_static_private_set ##### -->
1569 Sets the pointer keyed to @private_key for the current thread and the
1570 function @notify to be called with that pointer (%NULL or non-%NULL),
1571 whenever the pointer is set again or whenever the current thread ends.
1575 This function also works, if g_thread_init() has not yet been
1576 called. If g_thread_init() is called later, the @data keyed to
1577 @private_key will be inherited only by the main thread, i.e. the one that
1578 called g_thread_init().
1583 @notify is working quite differently from @destructor in
1588 @private_key: a #GStaticPrivate.
1589 @data: the new pointer.
1590 @notify: a function to be called with the pointer, whenever the
1591 current thread ends or sets this pointer again.
1594 <!-- ##### FUNCTION g_static_private_free ##### -->
1596 Releases all resources allocated to @private_key.
1600 You don't have to call this functions for a #GStaticPrivate with an
1601 unbounded lifetime, i.e. objects declared 'static', but if you have a
1602 #GStaticPrivate as a member of a structure and the structure is freed,
1603 you should also free the #GStaticPrivate.
1606 @private_key: a #GStaticPrivate to be freed.
1609 <!-- ##### STRUCT GOnce ##### -->
1611 A <structname>GOnce</structname> struct controls a one-time initialization function.
1612 Any one-time initialization function must have its own unique <structname>GOnce</structname>
1618 <!-- ##### ENUM GOnceStatus ##### -->
1620 The possible stati of a one-time initialization function controlled by a #GOnce struct.
1623 @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
1624 @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
1625 @G_ONCE_STATUS_READY: the function has been called.
1627 <!-- ##### MACRO G_ONCE_INIT ##### -->
1629 A #GOnce must be initialized with this macro, before it can be used.
1634 GOnce my_once = G_ONCE_INIT;
1641 <!-- ##### MACRO g_once ##### -->
1643 The first call to this routine by a process with a given #GOnce struct calls @func with the given
1644 argument. Thereafter, subsequent calls to g_once() with the same #GOnce struct do not call @func
1645 again, but return the stored result of the first call. On return from g_once(), the status of @once
1646 will be %G_ONCE_STATUS_READY.
1649 For example, a mutex or a thread-specific data key must be created exactly once. In a threaded
1650 environment, calling g_once() ensures that the initialization is serialized across multiple threads.
1653 Calling g_once() recursively on the same #GOnce struct in @func will lead to a deadlock.
1661 static GOnce my_once = G_ONCE_INIT;
1663 g_once (&my_once, parse_debug_flags, NULL);
1665 return my_once.retval;
1671 @once: a #GOnce structure
1672 @func: the function associated to @once. This function is called only once, regardless of the
1673 number of times it and its associated #GOnce struct are passed to g_once() .
1674 @arg: data to be passed to @func