glib-Threads.html   [plain text]


<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Threads</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-core.html" title="GLib Core Application Support"><link rel="previous" href="glib-The-Main-Event-Loop.html" title="The Main Event Loop"><link rel="next" href="glib-Thread-Pools.html" title="Thread Pools"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="refentry" href="glib-building.html" title="Compiling the GLib package"><link rel="refentry" href="glib-cross-compiling.html" title="Cross-compiling the GLib package"><link rel="refentry" href="glib-compiling.html" title="Compiling GLib Applications"><link rel="refentry" href="glib-running.html" title="Running GLib Applications"><link rel="refentry" href="glib-changes.html" title="Changes to GLib"><link rel="refentry" href="glib-resources.html" title="Mailing lists and bug reports"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="refentry" href="glib-Basic-Types.html" title="Basic Types"><link rel="refentry" href="glib-Limits-of-Basic-Types.html" title="Limits of Basic Types"><link rel="refentry" href="glib-Standard-Macros.html" title="Standard Macros"><link rel="refentry" href="glib-Type-Conversion-Macros.html" title="Type Conversion Macros"><link rel="refentry" href="glib-Byte-Order-Macros.html" title="Byte Order Macros"><link rel="refentry" href="glib-Numerical-Definitions.html" title="Numerical Definitions"><link rel="refentry" href="glib-Miscellaneous-Macros.html" title="Miscellaneous Macros"><link rel="refentry" href="glib-Atomic-Operations.html" title="Atomic Operations"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="refentry" href="glib-The-Main-Event-Loop.html" title="The Main Event Loop"><link rel="refentry" href="glib-Threads.html" title="
Threads"><link rel="refentry" href="glib-Thread-Pools.html" title="Thread Pools"><link rel="refentry" href="glib-Asynchronous-Queues.html" title="Asynchronous Queues"><link rel="refentry" href="glib-Dynamic-Loading-of-Modules.html" title="Dynamic Loading of Modules"><link rel="refentry" href="glib-Memory-Allocation.html" title="Memory Allocation"><link rel="refentry" href="glib-IO-Channels.html" title="IO Channels"><link rel="refentry" href="glib-Error-Reporting.html" title="Error Reporting"><link rel="refentry" href="glib-Warnings-and-Assertions.html" title="Message Output and Debugging Functions"><link rel="refentry" href="glib-Message-Logging.html" title="Message Logging"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="refentry" href="glib-String-Utility-Functions.html" title="String Utility Functions"><link rel="refentry" href="glib-Character-Set-Conversion.html" title="Character Set Conversion"><link rel="refentry" href="glib-Unicode-Manipulation.html" title="Unicode Manipulation"><link rel="refentry" href="glib-I18N.html" title="Internationalization"><link rel="refentry" href="glib-Date-and-Time-Functions.html" title="Date and Time Functions"><link rel="refentry" href="glib-Random-Numbers.html" title="Random Numbers"><link rel="refentry" href="glib-Hook-Functions.html" title="Hook Functions"><link rel="refentry" href="glib-Miscellaneous-Utility-Functions.html" title="Miscellaneous Utility Functions"><link rel="refentry" href="glib-Lexical-Scanner.html" title="Lexical Scanner"><link rel="refentry" href="glib-Automatic-String-Completion.html" title="Automatic String Completion"><link rel="refentry" href="glib-Timers.html" title="Timers"><link rel="refentry" href="glib-Spawning-Processes.html" title="Spawning Processes"><link rel="refentry" href="glib-File-Utilities.html" title="File Utilities"><link rel="refentry" href="glib-Shell-related-Utilities.html" title="Shell-related Utilities"><link rel="refentry" href="glib-Glob-style-pattern-matching.html" title="Glob-style pattern matching"><link rel="refentry" href="glib-Simple-XML-Subset-Parser.html" title="Simple XML Subset Parser"><link rel="refentry" href="glib-Windows-Compatability-Functions.html" title="Windows Compatibility Functions"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="refentry" href="glib-Memory-Chunks.html" title="Memory Chunks"><link rel="refentry" href="glib-Doubly-Linked-Lists.html" title="Doubly-Linked Lists"><link rel="refentry" href="glib-Singly-Linked-Lists.html" title="Singly-Linked Lists"><link rel="refentry" href="glib-Double-ended-Queues.html" title="Double-ended Queues"><link rel="refentry" href="glib-Trash-Stacks.html" title="Trash Stacks"><link rel="refentry" href="glib-Hash-Tables.html" title="Hash Tables"><link rel="refentry" href="glib-Strings.html" title="Strings"><link rel="refentry" href="glib-String-Chunks.html" title="String Chunks"><link rel="refentry" href="glib-Arrays.html" title="Arrays"><link rel="refentry" href="glib-Pointer-Arrays.html" title="Pointer Arrays"><link rel="refentry" href="glib-Byte-Arrays.html" title="Byte Arrays"><link rel="refentry" href="glib-Balanced-Binary-Trees.html" title="Balanced Binary Trees"><link rel="refentry" href="glib-N-ary-Trees.html" title="N-ary Trees"><link rel="refentry" href="glib-Quarks.html" title="Quarks"><link rel="refentry" href="glib-Keyed-Data-Lists.html" title="Keyed Data Lists"><link rel="refentry" href="glib-Datasets.html" title="Datasets"><link rel="refentry" href="glib-Relations-and-Tuples.html" title="Relations and Tuples"><link rel="refentry" href="glib-Caches.html" title="Caches"><link rel="refentry" href="glib-Memory-Allocators.html" title="Memory Allocators"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="refentry" href="glib-gettextize.html" title="glib-gettextize"><link rel="index" href="ix01.html" title="Index"><link rel="section" href="glib-Threads.html#id3027038" title="Description"><link rel="section" href="glib-Threads.html#id3027140" title="Details"><link rel="section" href="glib-Threads.html#id3036208" title="See Also"><meta name="generator" content="GTK-Doc V1.2 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-The-Main-Event-Loop.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-core.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Thread-Pools.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Threads"></a><div class="titlepage"><div></div><div></div></div><div class="refnamediv"><h2><span class="refentrytitle">
Threads</span></h2><p>
Threads &#8212; 
thread abstraction; including threads, different mutexes, conditions
and thread private data.</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">

#include &lt;glib.h&gt;



#define     <a href="glib-Threads.html#G-THREADS-ENABLED-CAPS">G_THREADS_ENABLED</a>
#define     <a href="glib-Threads.html#G-THREADS-IMPL-POSIX-CAPS">G_THREADS_IMPL_POSIX</a>
#define     <a href="glib-Threads.html#G-THREADS-IMPL-SOLARIS-CAPS">G_THREADS_IMPL_SOLARIS</a>
#define     <a href="glib-Threads.html#G-THREADS-IMPL-NONE-CAPS">G_THREADS_IMPL_NONE</a>

#define     <a href="glib-Threads.html#G-THREAD-ERROR-CAPS">G_THREAD_ERROR</a>
enum        <a href="glib-Threads.html#GThreadError">GThreadError</a>;

struct      <a href="glib-Threads.html#GThreadFunctions">GThreadFunctions</a>;
void        <a href="glib-Threads.html#g-thread-init">g_thread_init</a>                   (<a href="glib-Threads.html#GThreadFunctions">GThreadFunctions</a> *vtable);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-thread-supported">g_thread_supported</a>              ();

<a href="glib-Basic-Types.html#gpointer">gpointer</a>    (<a href="glib-Threads.html#GThreadFunc">*GThreadFunc</a>)                  (<a href="glib-Basic-Types.html#gpointer">gpointer</a> data);
enum        <a href="glib-Threads.html#GThreadPriority">GThreadPriority</a>;
struct      <a href="glib-Threads.html#GThread">GThread</a>;
<a href="glib-Threads.html#GThread">GThread</a>*    <a href="glib-Threads.html#g-thread-create">g_thread_create</a>                 (<a href="glib-Threads.html#GThreadFunc">GThreadFunc</a> func,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> joinable,
                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);
<a href="glib-Threads.html#GThread">GThread</a>*    <a href="glib-Threads.html#g-thread-create-full">g_thread_create_full</a>            (<a href="glib-Threads.html#GThreadFunc">GThreadFunc</a> func,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Basic-Types.html#gulong">gulong</a> stack_size,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> joinable,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> bound,
                                             <a href="glib-Threads.html#GThreadPriority">GThreadPriority</a> priority,
                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);
<a href="glib-Threads.html#GThread">GThread</a>*    <a href="glib-Threads.html#g-thread-self">g_thread_self</a>                   (void);
<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Threads.html#g-thread-join">g_thread_join</a>                   (<a href="glib-Threads.html#GThread">GThread</a> *thread);
void        <a href="glib-Threads.html#g-thread-set-priority">g_thread_set_priority</a>           (<a href="glib-Threads.html#GThread">GThread</a> *thread,
                                             <a href="glib-Threads.html#GThreadPriority">GThreadPriority</a> priority);
void        <a href="glib-Threads.html#g-thread-yield">g_thread_yield</a>                  ();
void        <a href="glib-Threads.html#g-thread-exit">g_thread_exit</a>                   (<a href="glib-Basic-Types.html#gpointer">gpointer</a> retval);

struct      <a href="glib-Threads.html#GMutex">GMutex</a>;
<a href="glib-Threads.html#GMutex">GMutex</a>*     <a href="glib-Threads.html#g-mutex-new">g_mutex_new</a>                     ();
void        <a href="glib-Threads.html#g-mutex-lock">g_mutex_lock</a>                    (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-mutex-trylock">g_mutex_trylock</a>                 (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);
void        <a href="glib-Threads.html#g-mutex-unlock">g_mutex_unlock</a>                  (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);
void        <a href="glib-Threads.html#g-mutex-free">g_mutex_free</a>                    (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);

struct      <a href="glib-Threads.html#GStaticMutex">GStaticMutex</a>;
#define     <a href="glib-Threads.html#G-STATIC-MUTEX-INIT-CAPS">G_STATIC_MUTEX_INIT</a>
void        <a href="glib-Threads.html#g-static-mutex-init">g_static_mutex_init</a>             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-mutex-lock">g_static_mutex_lock</a>             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-static-mutex-trylock">g_static_mutex_trylock</a>          (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-mutex-unlock">g_static_mutex_unlock</a>           (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);
<a href="glib-Threads.html#GMutex">GMutex</a>*     <a href="glib-Threads.html#g-static-mutex-get-mutex">g_static_mutex_get_mutex</a>        (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-mutex-free">g_static_mutex_free</a>             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);

#define     <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS">G_LOCK_DEFINE</a>                   (name)
#define     <a href="glib-Threads.html#G-LOCK-DEFINE-STATIC-CAPS">G_LOCK_DEFINE_STATIC</a>            (name)
#define     <a href="glib-Threads.html#G-LOCK-EXTERN-CAPS">G_LOCK_EXTERN</a>                   (name)
#define     <a href="glib-Threads.html#G-LOCK-CAPS">G_LOCK</a>                          (name)
#define     <a href="glib-Threads.html#G-TRYLOCK-CAPS">G_TRYLOCK</a>                       (name)
#define     <a href="glib-Threads.html#G-UNLOCK-CAPS">G_UNLOCK</a>                        (name)

struct      <a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a>;
#define     <a href="glib-Threads.html#G-STATIC-REC-MUTEX-INIT-CAPS">G_STATIC_REC_MUTEX_INIT</a>
void        <a href="glib-Threads.html#g-static-rec-mutex-init">g_static_rec_mutex_init</a>         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-rec-mutex-lock">g_static_rec_mutex_lock</a>         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-static-rec-mutex-trylock">g_static_rec_mutex_trylock</a>      (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-rec-mutex-unlock">g_static_rec_mutex_unlock</a>       (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-rec-mutex-lock-full">g_static_rec_mutex_lock_full</a>    (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex,
                                             <a href="glib-Basic-Types.html#guint">guint</a> depth);
<a href="glib-Basic-Types.html#guint">guint</a>       <a href="glib-Threads.html#g-static-rec-mutex-unlock-full">g_static_rec_mutex_unlock_full</a>  (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);
void        <a href="glib-Threads.html#g-static-rec-mutex-free">g_static_rec_mutex_free</a>         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);

struct      <a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a>;
#define     <a href="glib-Threads.html#G-STATIC-RW-LOCK-INIT-CAPS">G_STATIC_RW_LOCK_INIT</a>
void        <a href="glib-Threads.html#g-static-rw-lock-init">g_static_rw_lock_init</a>           (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
void        <a href="glib-Threads.html#g-static-rw-lock-reader-lock">g_static_rw_lock_reader_lock</a>    (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-static-rw-lock-reader-trylock">g_static_rw_lock_reader_trylock</a> (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
void        <a href="glib-Threads.html#g-static-rw-lock-reader-unlock">g_static_rw_lock_reader_unlock</a>  (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
void        <a href="glib-Threads.html#g-static-rw-lock-writer-lock">g_static_rw_lock_writer_lock</a>    (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-static-rw-lock-writer-trylock">g_static_rw_lock_writer_trylock</a> (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
void        <a href="glib-Threads.html#g-static-rw-lock-writer-unlock">g_static_rw_lock_writer_unlock</a>  (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);
void        <a href="glib-Threads.html#g-static-rw-lock-free">g_static_rw_lock_free</a>           (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);

struct      <a href="glib-Threads.html#GCond">GCond</a>;
<a href="glib-Threads.html#GCond">GCond</a>*      <a href="glib-Threads.html#g-cond-new">g_cond_new</a>                      ();
void        <a href="glib-Threads.html#g-cond-signal">g_cond_signal</a>                   (<a href="glib-Threads.html#GCond">GCond</a> *cond);
void        <a href="glib-Threads.html#g-cond-broadcast">g_cond_broadcast</a>                (<a href="glib-Threads.html#GCond">GCond</a> *cond);
void        <a href="glib-Threads.html#g-cond-wait">g_cond_wait</a>                     (<a href="glib-Threads.html#GCond">GCond</a> *cond,
                                             <a href="glib-Threads.html#GMutex">GMutex</a> *mutex);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Threads.html#g-cond-timed-wait">g_cond_timed_wait</a>               (<a href="glib-Threads.html#GCond">GCond</a> *cond,
                                             <a href="glib-Threads.html#GMutex">GMutex</a> *mutex,
                                             <a href="glib-Date-and-Time-Functions.html#GTimeVal">GTimeVal</a> *abs_time);
void        <a href="glib-Threads.html#g-cond-free">g_cond_free</a>                     (<a href="glib-Threads.html#GCond">GCond</a> *cond);

struct      <a href="glib-Threads.html#GPrivate">GPrivate</a>;
<a href="glib-Threads.html#GPrivate">GPrivate</a>*   <a href="glib-Threads.html#g-private-new">g_private_new</a>                   (<a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destructor);
<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Threads.html#g-private-get">g_private_get</a>                   (<a href="glib-Threads.html#GPrivate">GPrivate</a> *private_key);
void        <a href="glib-Threads.html#g-private-set">g_private_set</a>                   (<a href="glib-Threads.html#GPrivate">GPrivate</a> *private_key,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);

struct      <a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a>;
#define     <a href="glib-Threads.html#G-STATIC-PRIVATE-INIT-CAPS">G_STATIC_PRIVATE_INIT</a>
void        <a href="glib-Threads.html#g-static-private-init">g_static_private_init</a>           (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);
<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Threads.html#g-static-private-get">g_static_private_get</a>            (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);
void        <a href="glib-Threads.html#g-static-private-set">g_static_private_set</a>            (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);
void        <a href="glib-Threads.html#g-static-private-free">g_static_private_free</a>           (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);

struct      <a href="glib-Threads.html#GOnce">GOnce</a>;
enum        <a href="glib-Threads.html#GOnceStatus">GOnceStatus</a>;
#define     <a href="glib-Threads.html#G-ONCE-INIT-CAPS">G_ONCE_INIT</a>
#define     <a href="glib-Threads.html#g-once">g_once</a>                          (once, func, arg)

</pre></div><div class="refsect1" lang="en"><a name="id3027038"></a><h2>Description</h2><p>
Threads act almost like processes, but unlike processes all threads of
one process share the same memory. This is good, as it provides easy
communication between the involved threads via this shared memory, and
it is bad, because strange things (so called Heisenbugs) might happen,
when the program is not carefully designed. Especially bad is, that due
to the concurrent nature of threads no assumptions on the order of
execution of different threads can be done unless explicitly forced by
the programmer through synchronization primitives.
</p><p>
The aim of the thread related functions in GLib is to provide a
portable means for writing multi-threaded software. There are
primitives for mutexes to protect the access to portions of memory
(<a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>, <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a>, <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> and
<a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a>), there are primitives for condition variables to allow
synchronization of threads (<a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>) and finally there are primitives
for thread-private data, that every thread has a private instance of
(<a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>, <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>). Last but definitely not least there are
primitives to portably create and manage threads (<a href="glib-Threads.html#GThread"><span class="type">GThread</span></a>).
</p></div><div class="refsect1" lang="en"><a name="id3027140"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3027146"></a><h3><a name="G-THREADS-ENABLED-CAPS"></a>G_THREADS_ENABLED</h3><a class="indexterm" name="id3027158"></a><pre class="programlisting">#define G_THREADS_ENABLED
</pre><p>
This macro is defined, if GLib was compiled with thread support. This
does not necessarily mean, that there is a thread implementation
available, but the infrastructure is in place and once you provide a
thread implementation to <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>, GLib will be multi-thread
safe. It isn't and cannot be, if <a href="glib-Threads.html#G-THREADS-ENABLED-CAPS"><span class="type">G_THREADS_ENABLED</span></a> is not defined.
</p></div><hr><div class="refsect2" lang="en"><a name="id3027197"></a><h3><a name="G-THREADS-IMPL-POSIX-CAPS"></a>G_THREADS_IMPL_POSIX</h3><a class="indexterm" name="id3027208"></a><pre class="programlisting">#define G_THREADS_IMPL_POSIX
</pre><p>
This macro is defined, if POSIX style threads are used.
</p></div><hr><div class="refsect2" lang="en"><a name="id3027225"></a><h3><a name="G-THREADS-IMPL-SOLARIS-CAPS"></a>G_THREADS_IMPL_SOLARIS</h3><a class="indexterm" name="id3027237"></a><pre class="programlisting">#define G_THREADS_IMPL_SOLARIS
</pre><p>
This macro is defined, if the Solaris thread system is used.
</p></div><hr><div class="refsect2" lang="en"><a name="id3027253"></a><h3><a name="G-THREADS-IMPL-NONE-CAPS"></a>G_THREADS_IMPL_NONE</h3><a class="indexterm" name="id3027265"></a><pre class="programlisting">#define G_THREADS_IMPL_NONE
</pre><p>
This macro is defined, if no thread implementation is used. You can
however provide one to <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> to make GLib multi-thread safe.
</p></div><hr><div class="refsect2" lang="en"><a name="id3027293"></a><h3><a name="G-THREAD-ERROR-CAPS"></a>G_THREAD_ERROR</h3><a class="indexterm" name="id3027304"></a><pre class="programlisting">#define G_THREAD_ERROR g_thread_error_quark ()
</pre><p>
The error domain of the GLib thread subsystem.
</p></div><hr><div class="refsect2" lang="en"><a name="id3027320"></a><h3><a name="GThreadError"></a>enum GThreadError</h3><a class="indexterm" name="id3027331"></a><pre class="programlisting">typedef enum
{
  G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
} GThreadError;
</pre><p>
Possible errors of thread related functions.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><tt class="literal">G_THREAD_ERROR_AGAIN</tt></span></td><td>a thread couldn't be created due to resource
shortage. Try again later.

</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3027369"></a><h3><a name="GThreadFunctions"></a>struct GThreadFunctions</h3><a class="indexterm" name="id3027380"></a><pre class="programlisting">struct GThreadFunctions {

  GMutex*  (*mutex_new)           (void);
  void     (*mutex_lock)          (GMutex               *mutex);
  gboolean (*mutex_trylock)       (GMutex               *mutex);
  void     (*mutex_unlock)        (GMutex               *mutex);
  void     (*mutex_free)          (GMutex               *mutex);
  GCond*   (*cond_new)            (void);
  void     (*cond_signal)         (GCond                *cond);
  void     (*cond_broadcast)      (GCond                *cond);
  void     (*cond_wait)           (GCond                *cond,
                                   GMutex               *mutex);
  gboolean (*cond_timed_wait)     (GCond                *cond,
                                   GMutex               *mutex,
                                   GTimeVal             *end_time);
  void      (*cond_free)          (GCond                *cond);
  GPrivate* (*private_new)        (GDestroyNotify        destructor);
  gpointer  (*private_get)        (GPrivate             *private_key);
  void      (*private_set)        (GPrivate             *private_key,
                                   gpointer              data);
  void      (*thread_create)      (GThreadFunc           func,
                                   gpointer              data,
                                   gulong                stack_size,
                                   gboolean              joinable,
                                   gboolean              bound,
                                   GThreadPriority       priority,
                                   gpointer              thread,
                                   GError              **error);
  void      (*thread_yield)       (void);
  void      (*thread_join)        (gpointer              thread);
  void      (*thread_exit)        (void);
  void      (*thread_set_priority)(gpointer              thread,
                                   GThreadPriority       priority);
  void      (*thread_self)        (gpointer              thread);
  gboolean  (*thread_equal)       (gpointer              thread1,
				   gpointer              thread2);
};
</pre><p>
This function table is used by <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> to initialize the
thread system. The functions in that table are directly used by their
g_* prepended counterparts, that are described here, e.g. if you call
<a href="glib-Threads.html#g-mutex-new"><tt class="function">g_mutex_new()</tt></a> then <tt class="function">mutex_new()</tt> from the table provided to
<a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> will be called.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
This struct should only be used, if you know, what you are doing.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3027484"></a><h3><a name="g-thread-init"></a>g_thread_init ()</h3><a class="indexterm" name="id3027495"></a><pre class="programlisting">void        g_thread_init                   (<a href="glib-Threads.html#GThreadFunctions">GThreadFunctions</a> *vtable);</pre><p>
Before you use a thread related function in GLib, you should
initialize the thread system. This is done by calling
<a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>. Most of the time you will only have to call
<tt class="literal">g_thread_init(NULL)</tt>. 
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
You should only call <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> with a non-<tt class="literal">NULL</tt> parameter if you
really know what you are doing.
</p></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
<a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> must not be called directly or indirectly as a
callback from GLib. Also no mutexes may be currently locked, while
calling <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>.
</p></div><p>
<a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> might only be called once. On the second call
it will abort with an error. If you want to make sure, that the thread
system is initialized, you can do that too:
</p><p>
</p><div class="informalexample"><pre class="programlisting">
if (!g_thread_supported ()) g_thread_init (NULL);
</pre></div><p>
</p><p>
After that line either the thread system is initialized or the program
will abort, if no thread system is available in GLib, i.e. either
<a href="glib-Threads.html#G-THREADS-ENABLED-CAPS"><span class="type">G_THREADS_ENABLED</span></a> is not defined or <a href="glib-Threads.html#G-THREADS-IMPL-NONE-CAPS"><span class="type">G_THREADS_IMPL_NONE</span></a> is defined.
</p><p>
If no thread system is available and <i class="parameter"><tt>vtable</tt></i> is <tt class="literal">NULL</tt> or if not all
elements of <i class="parameter"><tt>vtable</tt></i> are non-<tt class="literal">NULL</tt>, then <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> will abort.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
To use <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> in your program, you have to link with the
libraries that the command <span><b class="command">pkg-config --libs gthread-2.0</b></span> 
outputs. This is not the case for all the other thread related functions of
GLib. Those can be used without having to link with the thread libraries.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>vtable</tt></i> :</span></td><td>a function table of type <a href="glib-Threads.html#GThreadFunctions"><span class="type">GThreadFunctions</span></a>, that provides the
entry points to the thread system to be used.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3027746"></a><h3><a name="g-thread-supported"></a>g_thread_supported ()</h3><a class="indexterm" name="id3027757"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_thread_supported              ();</pre><p>
This function returns, whether the thread system is initialized or
not.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
This function is actually a macro. Apart from taking the address of it
you can however use it as if it was a function.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if the thread system is initialized.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3027807"></a><h3><a name="GThreadFunc"></a>GThreadFunc ()</h3><a class="indexterm" name="id3027818"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    (*GThreadFunc)                  (<a href="glib-Basic-Types.html#gpointer">gpointer</a> data);</pre><p>
Specifies the type of the <i class="parameter"><tt>func</tt></i> functions passed to
<a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a> or <a href="glib-Threads.html#g-thread-create-full"><tt class="function">g_thread_create_full()</tt></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>data</tt></i> :</span></td><td>data passed to the thread.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the return value of the thread, which will be returned by
<a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3027914"></a><h3><a name="GThreadPriority"></a>enum GThreadPriority</h3><a class="indexterm" name="id3027925"></a><pre class="programlisting">typedef enum
{
  G_THREAD_PRIORITY_LOW,
  G_THREAD_PRIORITY_NORMAL,
  G_THREAD_PRIORITY_HIGH,
  G_THREAD_PRIORITY_URGENT
} GThreadPriority;
</pre><p>
Specifies the priority of a thread. 
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
It is not guaranteed, that threads with different priorities really
behave accordingly. On some systems (e.g. Linux) there are no thread
priorities. On other systems (e.g. Solaris) there doesn't seem to be
different scheduling for different priorities. All in all try to avoid
being dependent on priorities.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><tt class="literal">G_THREAD_PRIORITY_LOW</tt></span></td><td>a priority lower than normal
</td></tr><tr><td><span class="term"><tt class="literal">G_THREAD_PRIORITY_NORMAL</tt></span></td><td>the default priority
</td></tr><tr><td><span class="term"><tt class="literal">G_THREAD_PRIORITY_HIGH</tt></span></td><td>a priority higher than normal
</td></tr><tr><td><span class="term"><tt class="literal">G_THREAD_PRIORITY_URGENT</tt></span></td><td>the highest priority

</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3028019"></a><h3><a name="GThread"></a>struct GThread</h3><a class="indexterm" name="id3028030"></a><pre class="programlisting">struct GThread {

};
</pre><p>
The <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> struct represents a running thread. It has three public
read-only members, but the underlying struct is bigger, so you must
not copy this struct.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Resources for a joinable thread are not fully released until
<a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a> is called for that thread.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3028074"></a><h3><a name="g-thread-create"></a>g_thread_create ()</h3><a class="indexterm" name="id3028085"></a><pre class="programlisting"><a href="glib-Threads.html#GThread">GThread</a>*    g_thread_create                 (<a href="glib-Threads.html#GThreadFunc">GThreadFunc</a> func,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> joinable,
                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);</pre><p>
This function creates a new thread with the default priority.
</p><p>
If <i class="parameter"><tt>joinable</tt></i> is <tt class="literal">TRUE</tt>, you can wait for this threads termination
calling <a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a>. Otherwise the thread will just disappear, when
ready. 
</p><p>
The new thread executes the function <i class="parameter"><tt>func</tt></i> with the argument
<i class="parameter"><tt>data</tt></i>. If the thread was created successfully, it is returned.
</p><p>
<i class="parameter"><tt>error</tt></i> can be <tt class="literal">NULL</tt> to ignore errors, or non-<tt class="literal">NULL</tt> to report errors. The
error is set, if and only if the function returns <tt class="literal">NULL</tt>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>func</tt></i> :</span></td><td>a function to execute in the new thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>data</tt></i> :</span></td><td>an argument to supply to the new thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>joinable</tt></i> :</span></td><td>should this thread be joinable?
</td></tr><tr><td><span class="term"><i class="parameter"><tt>error</tt></i> :</span></td><td>return location for error.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> on success.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3028290"></a><h3><a name="g-thread-create-full"></a>g_thread_create_full ()</h3><a class="indexterm" name="id3028302"></a><pre class="programlisting"><a href="glib-Threads.html#GThread">GThread</a>*    g_thread_create_full            (<a href="glib-Threads.html#GThreadFunc">GThreadFunc</a> func,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Basic-Types.html#gulong">gulong</a> stack_size,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> joinable,
                                             <a href="glib-Basic-Types.html#gboolean">gboolean</a> bound,
                                             <a href="glib-Threads.html#GThreadPriority">GThreadPriority</a> priority,
                                             <a href="glib-Error-Reporting.html#GError">GError</a> **error);</pre><p>
This function creates a new thread with the priority <i class="parameter"><tt>priority</tt></i>. The
stack gets the size <i class="parameter"><tt>stack_size</tt></i> or the default value for the current
platform, if <i class="parameter"><tt>stack_size</tt></i> is 0.
</p><p>
If <i class="parameter"><tt>joinable</tt></i> is <tt class="literal">TRUE</tt>, you can wait for this threads termination
calling <a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a>. Otherwise the thread will just disappear, when
ready. If <i class="parameter"><tt>bound</tt></i> is <tt class="literal">TRUE</tt>, this thread will be scheduled in the system
scope, otherwise the implementation is free to do scheduling in the
process scope. The first variant is more expensive resource-wise, but
generally faster. On some systems (e.g. Linux) all threads are bound.
</p><p>
The new thread executes the function <i class="parameter"><tt>func</tt></i> with the argument
<i class="parameter"><tt>data</tt></i>. If the thread was created successfully, it is returned.
</p><p>
<i class="parameter"><tt>error</tt></i> can be <tt class="literal">NULL</tt> to ignore errors, or non-<tt class="literal">NULL</tt> to report errors. The
error is set, if and only if the function returns <tt class="literal">NULL</tt>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
It is not guaranteed, that threads with different priorities really
behave accordingly. On some systems (e.g. Linux) there are no thread
priorities. On other systems (e.g. Solaris) there doesn't seem to be
different scheduling for different priorities. All in all try to avoid
being dependent on priorities. Use <tt class="literal">G_THREAD_PRIORITY_NORMAL</tt> here as a
default.
</p></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Only use <a href="glib-Threads.html#g-thread-create-full"><tt class="function">g_thread_create_full()</tt></a>, when you really can't use
<a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a> instead. <a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a> does not take
<i class="parameter"><tt>stack_size</tt></i>, <i class="parameter"><tt>bound</tt></i> and <i class="parameter"><tt>priority</tt></i> as arguments, as they should only be
used for cases, where it is inevitable. 
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>func</tt></i> :</span></td><td>a function to execute in the new thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>data</tt></i> :</span></td><td>an argument to supply to the new thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>stack_size</tt></i> :</span></td><td>a stack size for the new thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>joinable</tt></i> :</span></td><td>should this thread be joinable?
</td></tr><tr><td><span class="term"><i class="parameter"><tt>bound</tt></i> :</span></td><td>should this thread be bound to a system thread?
</td></tr><tr><td><span class="term"><i class="parameter"><tt>priority</tt></i> :</span></td><td>a priority for the thread.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>error</tt></i> :</span></td><td>return location for error.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> on success.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3028684"></a><h3><a name="g-thread-self"></a>g_thread_self ()</h3><a class="indexterm" name="id3028695"></a><pre class="programlisting"><a href="glib-Threads.html#GThread">GThread</a>*    g_thread_self                   (void);</pre><p>
This functions returns the <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> corresponding to the calling thread.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the current thread.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3028740"></a><h3><a name="g-thread-join"></a>g_thread_join ()</h3><a class="indexterm" name="id3028752"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_thread_join                   (<a href="glib-Threads.html#GThread">GThread</a> *thread);</pre><p>
Waits until <i class="parameter"><tt>thread</tt></i> finishes, i.e. the function <i class="parameter"><tt>func</tt></i>, as given
to <a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a>, returns or <a href="glib-Threads.html#g-thread-exit"><tt class="function">g_thread_exit()</tt></a> is called by
<i class="parameter"><tt>thread</tt></i>. All resources of <i class="parameter"><tt>thread</tt></i> including the <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> struct are
released. <i class="parameter"><tt>thread</tt></i> must have been created with <i class="parameter"><tt>joinable</tt></i>=<tt class="literal">TRUE</tt> in
<a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a>. The value returned by <i class="parameter"><tt>func</tt></i> or given to
<a href="glib-Threads.html#g-thread-exit"><tt class="function">g_thread_exit()</tt></a> by <i class="parameter"><tt>thread</tt></i> is returned by this function.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>thread</tt></i> :</span></td><td>a <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a> to be waited for.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the return value of the thread.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3028923"></a><h3><a name="g-thread-set-priority"></a>g_thread_set_priority ()</h3><a class="indexterm" name="id3028935"></a><pre class="programlisting">void        g_thread_set_priority           (<a href="glib-Threads.html#GThread">GThread</a> *thread,
                                             <a href="glib-Threads.html#GThreadPriority">GThreadPriority</a> priority);</pre><p>
Changes the priority of <i class="parameter"><tt>thread</tt></i> to <i class="parameter"><tt>priority</tt></i>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
It is not guaranteed, that threads with different priorities really
behave accordingly. On some systems (e.g. Linux) there are no thread
priorities. On other systems (e.g. Solaris) there doesn't seem to be
different scheduling for different priorities. All in all try to avoid
being dependent on priorities.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>thread</tt></i> :</span></td><td>a <a href="glib-Threads.html#GThread"><span class="type">GThread</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>priority</tt></i> :</span></td><td>a new priority for <i class="parameter"><tt>thread</tt></i>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029041"></a><h3><a name="g-thread-yield"></a>g_thread_yield ()</h3><a class="indexterm" name="id3029051"></a><pre class="programlisting">void        g_thread_yield                  ();</pre><p>
Gives way to other threads waiting to be scheduled. 
</p><p>
This function is often used as a method to make busy wait less
evil. But in most cases, you will encounter, there are better methods
to do that. So in general you shouldn't use that function.
</p></div><hr><div class="refsect2" lang="en"><a name="id3029080"></a><h3><a name="g-thread-exit"></a>g_thread_exit ()</h3><a class="indexterm" name="id3029091"></a><pre class="programlisting">void        g_thread_exit                   (<a href="glib-Basic-Types.html#gpointer">gpointer</a> retval);</pre><p>
Exits the current thread. If another thread is waiting for that thread
using <a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a> and the current thread is joinable, the waiting
thread will be woken up and getting <i class="parameter"><tt>retval</tt></i> as the return value of
<a href="glib-Threads.html#g-thread-join"><tt class="function">g_thread_join()</tt></a>. If the current thread is not joinable, <i class="parameter"><tt>retval</tt></i> is
ignored. Calling
</p><p>
</p><div class="informalexample"><pre class="programlisting">
g_thread_exit (retval);
</pre></div><p>
</p><p>
is equivalent to calling 
</p><p>
</p><div class="informalexample"><pre class="programlisting">
return retval;
</pre></div><p>
</p><p>
in the function <i class="parameter"><tt>func</tt></i>, as given to <a href="glib-Threads.html#g-thread-create"><tt class="function">g_thread_create()</tt></a>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Never call <a href="glib-Threads.html#g-thread-exit"><tt class="function">g_thread_exit()</tt></a> from within a thread of a <a href="glib-Thread-Pools.html#GThreadPool"><span class="type">GThreadPool</span></a>, as
that will mess up the bookkeeping and lead to funny and unwanted results.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>retval</tt></i> :</span></td><td>the return value of this thread.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029252"></a><h3><a name="GMutex"></a>struct GMutex</h3><a class="indexterm" name="id3029263"></a><pre class="programlisting">struct GMutex;</pre><p>
The <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> struct is an opaque data structure to represent a mutex
(mutual exclusion). It can be used to protect data against shared
access. Take for example the following function:

</p><div class="example"><a name="id3029287"></a><p class="title"><b>Example 3. A function which will not work in a threaded environment</b></p><pre class="programlisting">
  int give_me_next_number ()
  {
    static int current_number = 0;

    /* now do a very complicated calculation to calculate the new number,
       this might for example be a random number generator */
    current_number = calc_next_number (current_number); 
    return current_number;
  }
</pre></div><p>
</p><p>
It is easy to see, that this won't work in a multi-threaded
application. There current_number must be protected against shared
access. A first naive implementation would be:
</p><p>
</p><div class="example"><a name="id3029318"></a><p class="title"><b>Example 4. The wrong way to write a thread-safe function</b></p><pre class="programlisting">
  int give_me_next_number ()
  {
    static int current_number = 0;
    int ret_val;
    static GMutex * mutex = NULL;

    if (!mutex)
      mutex = g_mutex_new ();
    g_mutex_lock (mutex);
    ret_val = current_number = calc_next_number (current_number); 
    g_mutex_unlock (mutex);
    return ret_val;
  }
</pre></div><p>
</p><p>
This looks like it would work, but there is a race condition while
constructing the mutex and this code cannot work reliable. So please do
not use such constructs in your own programs. One working solution is:
</p><p>
</p><div class="example"><a name="id3029352"></a><p class="title"><b>Example 5. A correct thread-safe function</b></p><pre class="programlisting">
  static GMutex *give_me_next_number_mutex = NULL;

  /* this function must be called before any call to give_me_next_number ()
     it must be called exactly once. */
  void init_give_me_next_number () 
  {
    g_assert (give_me_next_number_mutex == NULL);
    give_me_next_number_mutex = g_mutex_new ();
  }

  int give_me_next_number ()
  {
    static int current_number = 0;
    int ret_val;

    g_mutex_lock (give_me_next_number_mutex);
    ret_val = current_number = calc_next_number (current_number); 
    g_mutex_unlock (give_me_next_number_mutex);
    return ret_val;
  }
</pre></div><p>
</p><p>
<a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> provides a simpler and safer way of doing this.
</p><p>
If you want to use a mutex, but your code should also work without
calling <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> first, you can not use a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, as
<a href="glib-Threads.html#g-mutex-new"><tt class="function">g_mutex_new()</tt></a> requires that. Use a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> instead.
</p><p>
A <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> should only be accessed via the following functions.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
All of the <tt class="function">g_mutex_*</tt> functions are actually macros. 
Apart from taking their addresses, you can however use them as if they 
were functions.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3029466"></a><h3><a name="g-mutex-new"></a>g_mutex_new ()</h3><a class="indexterm" name="id3029477"></a><pre class="programlisting"><a href="glib-Threads.html#GMutex">GMutex</a>*     g_mutex_new                     ();</pre><p>
Creates a new <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>. 
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
This function will abort, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not been called yet.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a new <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029546"></a><h3><a name="g-mutex-lock"></a>g_mutex_lock ()</h3><a class="indexterm" name="id3029557"></a><pre class="programlisting">void        g_mutex_lock                    (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>
Locks <i class="parameter"><tt>mutex</tt></i>. If <i class="parameter"><tt>mutex</tt></i> is already locked by another thread, the
current thread will block until <i class="parameter"><tt>mutex</tt></i> is unlocked by the other
thread.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will do nothing then.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
<a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> is neither guaranteed to be recursive nor to be non-recursive,
i.e. a thread could deadlock while calling <a href="glib-Threads.html#g-mutex-lock"><tt class="function">g_mutex_lock()</tt></a>, if it
already has locked <i class="parameter"><tt>mutex</tt></i>. Use <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a>, if you need recursive
mutexes.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029687"></a><h3><a name="g-mutex-trylock"></a>g_mutex_trylock ()</h3><a class="indexterm" name="id3029698"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_mutex_trylock                 (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>
Tries to lock <i class="parameter"><tt>mutex</tt></i>. If <i class="parameter"><tt>mutex</tt></i> is already locked by another
thread, it immediately returns <tt class="literal">FALSE</tt>. Otherwise it locks <i class="parameter"><tt>mutex</tt></i>
and returns <tt class="literal">TRUE</tt>.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will immediately return <tt class="literal">TRUE</tt> then.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
<a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> is neither guaranteed to be recursive nor to be non-recursive,
i.e. the return value of <a href="glib-Threads.html#g-mutex-trylock"><tt class="function">g_mutex_trylock()</tt></a> could be both <tt class="literal">FALSE</tt> or
<tt class="literal">TRUE</tt>, if the current thread already has locked <i class="parameter"><tt>mutex</tt></i>. Use
<a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a>, if you need recursive mutexes.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if <i class="parameter"><tt>mutex</tt></i> could be locked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029880"></a><h3><a name="g-mutex-unlock"></a>g_mutex_unlock ()</h3><a class="indexterm" name="id3029891"></a><pre class="programlisting">void        g_mutex_unlock                  (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>
Unlocks <i class="parameter"><tt>mutex</tt></i>. If another thread is blocked in a <a href="glib-Threads.html#g-mutex-lock"><tt class="function">g_mutex_lock()</tt></a> call
for <i class="parameter"><tt>mutex</tt></i>, it will be woken and can lock <i class="parameter"><tt>mutex</tt></i> itself.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will do nothing then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029990"></a><h3><a name="g-mutex-free"></a>g_mutex_free ()</h3><a class="indexterm" name="id3030001"></a><pre class="programlisting">void        g_mutex_free                    (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>
Destroys <i class="parameter"><tt>mutex</tt></i>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030061"></a><h3><a name="GStaticMutex"></a>struct GStaticMutex</h3><a class="indexterm" name="id3030072"></a><pre class="programlisting">struct GStaticMutex;</pre><p>
A <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> works like a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, but it has one significant
advantage. It doesn't need to be created at run-time like a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>,
but can be defined at compile-time. Here is a shorter, easier and
safer version of our <tt class="function"><tt class="function">give_me_next_number()</tt></tt> example:
</p><p>
</p><div class="example"><a name="id3030133"></a><p class="title"><b>Example 6. Using <span class="structname">GStaticMutex</span> to simplify thread-safe programming</b></p><pre class="programlisting">
  int give_me_next_number ()
  {
    static int current_number = 0;
    int ret_val;
    static GStaticMutex mutex = G_STATIC_MUTEX_INIT;

    g_static_mutex_lock (&amp;mutex);
    ret_val = current_number = calc_next_number (current_number); 
    g_static_mutex_unlock (&amp;mutex);
    return ret_val;
  }
</pre></div><p>
</p><p>
Sometimes you would like to dynamically create a mutex. If you don't
want to require prior calling to <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>, because your code
should also be usable in non-threaded programs, you are not able to
use <a href="glib-Threads.html#g-mutex-new"><tt class="function">g_mutex_new()</tt></a> and thus <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, as that requires a prior call to
<a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>. In theses cases you can also use a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. It
must be initialized with <a href="glib-Threads.html#g-static-mutex-init"><tt class="function">g_static_mutex_init()</tt></a> before using it and
freed with with <a href="glib-Threads.html#g-static-mutex-free"><tt class="function">g_static_mutex_free()</tt></a> when not needed anymore to free
up any allocated resources.
</p><p>
Even though <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> is not opaque, it should only be used with
the following functions, as it is defined differently on different
platforms.
</p><p>
All of the <tt class="function">g_static_mutex_*</tt> functions can also be 
used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been called.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
All of the <tt class="function">g_static_mutex_*</tt> functions are actually 
macros. Apart from taking their addresses, you can however use them 
as if they were functions.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3030287"></a><h3><a name="G-STATIC-MUTEX-INIT-CAPS"></a>G_STATIC_MUTEX_INIT</h3><a class="indexterm" name="id3030299"></a><pre class="programlisting">#define G_STATIC_MUTEX_INIT
</pre><p>
A <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> must be initialized with this macro, before it can be
used. This macro can used be to initialize a variable, but it cannot
be assigned to a variable. In that case you have to use
<a href="glib-Threads.html#g-static-mutex-init"><tt class="function">g_static_mutex_init()</tt></a>.
</p><p>
</p><div class="informalexample"><pre class="programlisting">
GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
</pre></div><p>
</p></div><hr><div class="refsect2" lang="en"><a name="id3030348"></a><h3><a name="g-static-mutex-init"></a>g_static_mutex_init ()</h3><a class="indexterm" name="id3030360"></a><pre class="programlisting">void        g_static_mutex_init             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
Initializes <i class="parameter"><tt>mutex</tt></i>. Alternatively you can initialize it with
<a href="glib-Threads.html#G-STATIC-MUTEX-INIT-CAPS"><span class="type">G_STATIC_MUTEX_INIT</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> to be initialized.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030430"></a><h3><a name="g-static-mutex-lock"></a>g_static_mutex_lock ()</h3><a class="indexterm" name="id3030441"></a><pre class="programlisting">void        g_static_mutex_lock             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
Works like <a href="glib-Threads.html#g-mutex-lock"><tt class="function">g_mutex_lock()</tt></a>, but for a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030515"></a><h3><a name="g-static-mutex-trylock"></a>g_static_mutex_trylock ()</h3><a class="indexterm" name="id3030526"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_static_mutex_trylock          (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
Works like <a href="glib-Threads.html#g-mutex-trylock"><tt class="function">g_mutex_trylock()</tt></a>, but for a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if the <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> could be locked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030624"></a><h3><a name="g-static-mutex-unlock"></a>g_static_mutex_unlock ()</h3><a class="indexterm" name="id3030635"></a><pre class="programlisting">void        g_static_mutex_unlock           (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
Works like <a href="glib-Threads.html#g-mutex-unlock"><tt class="function">g_mutex_unlock()</tt></a>, but for a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030710"></a><h3><a name="g-static-mutex-get-mutex"></a>g_static_mutex_get_mutex ()</h3><a class="indexterm" name="id3030721"></a><pre class="programlisting"><a href="glib-Threads.html#GMutex">GMutex</a>*     g_static_mutex_get_mutex        (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
For some operations (like <a href="glib-Threads.html#g-cond-wait"><tt class="function">g_cond_wait()</tt></a>) you must have a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>
instead of a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. This function will return the
corresponding <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> for <i class="parameter"><tt>mutex</tt></i>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> corresponding to <i class="parameter"><tt>mutex</tt></i>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030842"></a><h3><a name="g-static-mutex-free"></a>g_static_mutex_free ()</h3><a class="indexterm" name="id3030854"></a><pre class="programlisting">void        g_static_mutex_free             (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>
Releases all resources allocated to <i class="parameter"><tt>mutex</tt></i>. 
</p><p>
You don't have to call this functions for a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> with an
unbounded lifetime, i.e. objects declared 'static', but if you have a
<a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> as a member of a structure and the structure is freed,
you should also free the <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a> to be freed.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3030946"></a><h3><a name="G-LOCK-DEFINE-CAPS"></a>G_LOCK_DEFINE()</h3><a class="indexterm" name="id3030957"></a><pre class="programlisting">#define     G_LOCK_DEFINE(name)</pre><p>
The <tt class="literal">G_LOCK_</tt>* macros provide a convenient interface to <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>
with the advantage that they will expand to nothing in programs
compiled against a thread-disabled GLib, saving code and memory
there. <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a> defines a lock. It can appear, where variable
definitions may appear in programs, i.e. in the first block of a
function or outside of functions. The <i class="parameter"><tt>name</tt></i> parameter will be mangled
to get the name of the <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. This means, that you can use
names of existing variables as the parameter, e.g. the name of the
variable you intent to protect with the lock. Look at our
<tt class="function"><tt class="function">give_me_next_number()</tt></tt> example using the <tt class="literal">G_LOCK_</tt>* macros:
</p><p>
</p><div class="example"><a name="id3031040"></a><p class="title"><b>Example 7. Using the <tt class="literal">G_LOCK_</tt>* convenience macros</b></p><pre class="programlisting">
G_LOCK_DEFINE (current_number);

int give_me_next_number ()
  {
    static int current_number = 0;
    int ret_val;

    G_LOCK (current_number);
    ret_val = current_number = calc_next_number (current_number); 
    G_UNLOCK (current_number);
    return ret_val;
  }
</pre></div><p>
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031086"></a><h3><a name="G-LOCK-DEFINE-STATIC-CAPS"></a>G_LOCK_DEFINE_STATIC()</h3><a class="indexterm" name="id3031097"></a><pre class="programlisting">#define     G_LOCK_DEFINE_STATIC(name)</pre><p>
This works like <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a>, but it creates a static object.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031140"></a><h3><a name="G-LOCK-EXTERN-CAPS"></a>G_LOCK_EXTERN()</h3><a class="indexterm" name="id3031151"></a><pre class="programlisting">#define     G_LOCK_EXTERN(name)</pre><p>
This declares a lock, that is defined with <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a> in another module.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031194"></a><h3><a name="G-LOCK-CAPS"></a>G_LOCK()</h3><a class="indexterm" name="id3031205"></a><pre class="programlisting">#define     G_LOCK(name)</pre><p>
Works like <a href="glib-Threads.html#g-mutex-lock"><tt class="function">g_mutex_lock()</tt></a>, but for a lock defined with <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031258"></a><h3><a name="G-TRYLOCK-CAPS"></a>G_TRYLOCK()</h3><a class="indexterm" name="id3031269"></a><pre class="programlisting">#define     G_TRYLOCK(name)</pre><p>
Works like <a href="glib-Threads.html#g-mutex-trylock"><tt class="function">g_mutex_trylock()</tt></a>, but for a lock defined with <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if the lock could be locked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031337"></a><h3><a name="G-UNLOCK-CAPS"></a>G_UNLOCK()</h3><a class="indexterm" name="id3031348"></a><pre class="programlisting">#define     G_UNLOCK(name)</pre><p>
Works like <a href="glib-Threads.html#g-mutex-unlock"><tt class="function">g_mutex_unlock()</tt></a>, but for a lock defined with <a href="glib-Threads.html#G-LOCK-DEFINE-CAPS"><span class="type">G_LOCK_DEFINE</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>name</tt></i> :</span></td><td>the name of the lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031401"></a><h3><a name="GStaticRecMutex"></a>struct GStaticRecMutex</h3><a class="indexterm" name="id3031413"></a><pre class="programlisting">struct GStaticRecMutex {

};
</pre><p>
A <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> works like a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>, but it can be locked
multiple times by one thread. If you enter it n times, however, you
have to unlock it n times again to let other threads lock it. An
exception is the function <a href="glib-Threads.html#g-static-rec-mutex-unlock-full"><tt class="function">g_static_rec_mutex_unlock_full()</tt></a>, that
allows you to unlock a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> completely returning the depth,
i.e. the number of times this mutex was locked. The depth can later be
used to restore the state by calling <a href="glib-Threads.html#g-static-rec-mutex-lock-full"><tt class="function">g_static_rec_mutex_lock_full()</tt></a>.
</p><p>
Even though <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> is not opaque, it should only be used with
the following functions.
</p><p>
All of the <tt class="function">g_static_rec_mutex_*</tt> functions can also 
be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not been called.
</p></div><hr><div class="refsect2" lang="en"><a name="id3031516"></a><h3><a name="G-STATIC-REC-MUTEX-INIT-CAPS"></a>G_STATIC_REC_MUTEX_INIT</h3><a class="indexterm" name="id3031528"></a><pre class="programlisting">#define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
</pre><p>
A <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> must be initialized with this macro, before it can
be used. This macro can used be to initialize a variable, but it
cannot be assigned to a variable. In that case you have to use
<a href="glib-Threads.html#g-static-rec-mutex-init"><tt class="function">g_static_rec_mutex_init()</tt></a>.
</p><p>
</p><div class="informalexample"><pre class="programlisting">
GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
</pre></div><p>
</p></div><hr><div class="refsect2" lang="en"><a name="id3031579"></a><h3><a name="g-static-rec-mutex-init"></a>g_static_rec_mutex_init ()</h3><a class="indexterm" name="id3031590"></a><pre class="programlisting">void        g_static_rec_mutex_init         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
A <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> must be initialized with this function, before it
can be used. Alternatively you can initialize it with
<a href="glib-Threads.html#G-STATIC-REC-MUTEX-INIT-CAPS"><span class="type">G_STATIC_REC_MUTEX_INIT</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to be initialized.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031664"></a><h3><a name="g-static-rec-mutex-lock"></a>g_static_rec_mutex_lock ()</h3><a class="indexterm" name="id3031675"></a><pre class="programlisting">void        g_static_rec_mutex_lock         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
Locks <i class="parameter"><tt>mutex</tt></i>. If <i class="parameter"><tt>mutex</tt></i> is already locked by another thread, the
current thread will block until <i class="parameter"><tt>mutex</tt></i> is unlocked by the other
thread. If <i class="parameter"><tt>mutex</tt></i> is already locked by the calling thread, this
functions increases the depth of <i class="parameter"><tt>mutex</tt></i> and returns immediately.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to lock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031764"></a><h3><a name="g-static-rec-mutex-trylock"></a>g_static_rec_mutex_trylock ()</h3><a class="indexterm" name="id3031775"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_static_rec_mutex_trylock      (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
Tries to lock <i class="parameter"><tt>mutex</tt></i>. If <i class="parameter"><tt>mutex</tt></i> is already locked by another thread,
it immediately returns <tt class="literal">FALSE</tt>. Otherwise it locks <i class="parameter"><tt>mutex</tt></i> and returns
<tt class="literal">TRUE</tt>. If <i class="parameter"><tt>mutex</tt></i> is already locked by the calling thread, this
functions increases the depth of <i class="parameter"><tt>mutex</tt></i> and immediately  returns <tt class="literal">TRUE</tt>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to lock.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if <i class="parameter"><tt>mutex</tt></i> could be locked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3031901"></a><h3><a name="g-static-rec-mutex-unlock"></a>g_static_rec_mutex_unlock ()</h3><a class="indexterm" name="id3031914"></a><pre class="programlisting">void        g_static_rec_mutex_unlock       (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
Unlocks <i class="parameter"><tt>mutex</tt></i>. Another threads can, however, only lock <i class="parameter"><tt>mutex</tt></i> when it
has been unlocked as many times, as it had been locked before. If
<i class="parameter"><tt>mutex</tt></i> is completely unlocked and another thread is blocked in a
<a href="glib-Threads.html#g-static-rec-mutex-lock"><tt class="function">g_static_rec_mutex_lock()</tt></a> call for <i class="parameter"><tt>mutex</tt></i>, it will be woken and can
lock <i class="parameter"><tt>mutex</tt></i> itself.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to unlock.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032012"></a><h3><a name="g-static-rec-mutex-lock-full"></a>g_static_rec_mutex_lock_full ()</h3><a class="indexterm" name="id3032024"></a><pre class="programlisting">void        g_static_rec_mutex_lock_full    (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex,
                                             <a href="glib-Basic-Types.html#guint">guint</a> depth);</pre><p>
Works like calling <a href="glib-Threads.html#g-static-rec-mutex-lock"><tt class="function">g_static_rec_mutex_lock()</tt></a> for <i class="parameter"><tt>mutex</tt></i> <i class="parameter"><tt>depth</tt></i> times.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to lock.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>depth</tt></i> :</span></td><td>number of times this mutex has to be unlocked to be completely unlocked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032125"></a><h3><a name="g-static-rec-mutex-unlock-full"></a>g_static_rec_mutex_unlock_full ()</h3><a class="indexterm" name="id3032137"></a><pre class="programlisting"><a href="glib-Basic-Types.html#guint">guint</a>       g_static_rec_mutex_unlock_full  (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
Completely unlocks <i class="parameter"><tt>mutex</tt></i>. If another thread is blocked in a
<a href="glib-Threads.html#g-static-rec-mutex-lock"><tt class="function">g_static_rec_mutex_lock()</tt></a> call for <i class="parameter"><tt>mutex</tt></i>, it will be woken and can
lock <i class="parameter"><tt>mutex</tt></i> itself. This function returns the number of times, that
<i class="parameter"><tt>mutex</tt></i> has been locked by the current thread. To restore the state
before the call to <a href="glib-Threads.html#g-static-rec-mutex-unlock-full"><tt class="function">g_static_rec_mutex_unlock_full()</tt></a> you can call
<a href="glib-Threads.html#g-static-rec-mutex-lock-full"><tt class="function">g_static_rec_mutex_lock_full()</tt></a> with the depth returned by this
function.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to completely unlock.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>number of times <i class="parameter"><tt>mutex</tt></i> has been locked by the current thread.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032271"></a><h3><a name="g-static-rec-mutex-free"></a>g_static_rec_mutex_free ()</h3><a class="indexterm" name="id3032283"></a><pre class="programlisting">void        g_static_rec_mutex_free         (<a href="glib-Threads.html#GStaticRecMutex">GStaticRecMutex</a> *mutex);</pre><p>
Releases all resources allocated to a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a>.
</p><p>
You don't have to call this functions for a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> with an
unbounded lifetime, i.e. objects declared 'static', but if you have a
<a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> as a member of a structure and the structure is
freed, you should also free the <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRecMutex"><span class="type">GStaticRecMutex</span></a> to be freed.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032378"></a><h3><a name="GStaticRWLock"></a>struct GStaticRWLock</h3><a class="indexterm" name="id3032389"></a><pre class="programlisting">struct GStaticRWLock {

};
</pre><p>
The <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> struct represents a read-write lock. A read-write
lock can be used for protecting data, that some portions of code only
read from, while others also write. In such situations it is
desirable, that several readers can read at once, whereas of course
only one writer may write at a time. Take a look at the following
example:

</p><div class="example"><a name="id3032417"></a><p class="title"><b>Example 8. An array with access functions</b></p><pre class="programlisting">
  GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;

  GPtrArray *array;

  gpointer my_array_get (guint index)
  {
    gpointer retval = NULL;

    if (!array)
      return NULL;

    g_static_rw_lock_reader_lock (&amp;rwlock);

    if (index &lt; array-&gt;len)
      retval = g_ptr_array_index (array, index);

    g_static_rw_lock_reader_unlock (&amp;rwlock);

    return retval;
  }

  void my_array_set (guint index, gpointer data)
  {
    g_static_rw_lock_writer_lock (&amp;rwlock);

    if (!array)
      array = g_ptr_array_new ();

    if (index &gt;= array-&gt;len)
      g_ptr_array_set_size (array, index+1);

    g_ptr_array_index (array, index) = data; 

    g_static_rw_lock_writer_unlock (&amp;rwlock);
  }
</pre></div><p>
</p><p>
This example shows an array, which can be accessed by many readers
(the <tt class="function"><tt class="function">my_array_get()</tt></tt> function) simultaneously, 
whereas the writers (the <tt class="function"><tt class="function">my_array_set()</tt></tt> function) 
will only be allowed once a time and only if no readers currently access 
the array. This is because of the potentially dangerous resizing of the 
array. Using these functions is fully multi-thread safe now. 
</p><p>
Most of the time the writers should have precedence of readers. That
means for this implementation, that as soon as a writer wants to lock
the data, no other reader is allowed to lock the data, whereas of
course the readers, that already have locked the data are allowed to
finish their operation. As soon as the last reader unlocks the data,
the writer will lock it.
</p><p>
Even though <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> is not opaque, it should only be used with
the following functions.
</p><p>
All of the <tt class="function">g_static_rw_lock_*</tt> functions can also be 
used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not been called.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
A read-write lock has a higher overhead as a mutex. For example both
<a href="glib-Threads.html#g-static-rw-lock-reader-lock"><tt class="function">g_static_rw_lock_reader_lock()</tt></a> and <a href="glib-Threads.html#g-static-rw-lock-reader-unlock"><tt class="function">g_static_rw_lock_reader_unlock()</tt></a>
have to lock and unlock a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>, so it takes at least twice the
time to lock and unlock a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> than to lock and unlock a
<a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. So only data structures, that are accessed by multiple
readers, which keep the lock for a considerable time justify a
<a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a>. The above example most probably would fare better with
a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3032617"></a><h3><a name="G-STATIC-RW-LOCK-INIT-CAPS"></a>G_STATIC_RW_LOCK_INIT</h3><a class="indexterm" name="id3032628"></a><pre class="programlisting">#define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
</pre><p>
A <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> must be initialized with this macro, before it can
be used. This macro can used be to initialize a variable, but it
cannot be assigned to a variable. In that case you have to use
<a href="glib-Threads.html#g-static-rw-lock-init"><tt class="function">g_static_rw_lock_init()</tt></a>.
</p><p>
</p><div class="informalexample"><pre class="programlisting">
GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
</pre></div><p>
</p></div><hr><div class="refsect2" lang="en"><a name="id3032680"></a><h3><a name="g-static-rw-lock-init"></a>g_static_rw_lock_init ()</h3><a class="indexterm" name="id3032691"></a><pre class="programlisting">void        g_static_rw_lock_init           (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
A <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> must be initialized with this function, before it can
be used. Alternatively you can initialize it with
<a href="glib-Threads.html#G-STATIC-RW-LOCK-INIT-CAPS"><span class="type">G_STATIC_RW_LOCK_INIT</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to be initialized.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032765"></a><h3><a name="g-static-rw-lock-reader-lock"></a>g_static_rw_lock_reader_lock ()</h3><a class="indexterm" name="id3032777"></a><pre class="programlisting">void        g_static_rw_lock_reader_lock    (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Locks <i class="parameter"><tt>lock</tt></i> for reading. There may be unlimited concurrent locks for
reading of a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> at the same time.  If <i class="parameter"><tt>lock</tt></i> is already
locked for writing by another thread or if another thread is already
waiting to lock <i class="parameter"><tt>lock</tt></i> for writing, this function will block until
<i class="parameter"><tt>lock</tt></i> is unlocked by the other writing thread and no other writing
threads want to lock <i class="parameter"><tt>lock</tt></i>. This lock has to be unlocked by
<a href="glib-Threads.html#g-static-rw-lock-reader-unlock"><tt class="function">g_static_rw_lock_reader_unlock()</tt></a>.
</p><p>
<a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> is not recursive. It might seem to be possible to
recursively lock for reading, but that can result in a deadlock as
well, due to writer preference.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to lock for reading.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3032901"></a><h3><a name="g-static-rw-lock-reader-trylock"></a>g_static_rw_lock_reader_trylock ()</h3><a class="indexterm" name="id3032913"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_static_rw_lock_reader_trylock (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Tries to lock <i class="parameter"><tt>lock</tt></i> for reading. If <i class="parameter"><tt>lock</tt></i> is already locked for
writing by another thread or if another thread is already waiting to
lock <i class="parameter"><tt>lock</tt></i> for writing, it immediately returns <tt class="literal">FALSE</tt>. Otherwise it
locks <i class="parameter"><tt>lock</tt></i> for reading and returns <tt class="literal">TRUE</tt>. This lock has to be unlocked
by <a href="glib-Threads.html#g-static-rw-lock-reader-unlock"><tt class="function">g_static_rw_lock_reader_unlock()</tt></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to lock for reading.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if <i class="parameter"><tt>lock</tt></i> could be locked for reading.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033040"></a><h3><a name="g-static-rw-lock-reader-unlock"></a>g_static_rw_lock_reader_unlock ()</h3><a class="indexterm" name="id3033052"></a><pre class="programlisting">void        g_static_rw_lock_reader_unlock  (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Unlocks <i class="parameter"><tt>lock</tt></i>. If a thread waits to lock <i class="parameter"><tt>lock</tt></i> for writing and all
locks for reading have been unlocked, the waiting thread is woken up
and can lock <i class="parameter"><tt>lock</tt></i> for writing.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to unlock after reading.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033128"></a><h3><a name="g-static-rw-lock-writer-lock"></a>g_static_rw_lock_writer_lock ()</h3><a class="indexterm" name="id3033140"></a><pre class="programlisting">void        g_static_rw_lock_writer_lock    (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Locks <i class="parameter"><tt>lock</tt></i> for writing. If <i class="parameter"><tt>lock</tt></i> is already locked for writing or
reading by other threads, this function will block until <i class="parameter"><tt>lock</tt></i> is
completely unlocked and then lock <i class="parameter"><tt>lock</tt></i> for writing. While this
functions waits to lock <i class="parameter"><tt>lock</tt></i>, no other thread can lock <i class="parameter"><tt>lock</tt></i> for
reading. When <i class="parameter"><tt>lock</tt></i> is locked for writing, no other thread can lock
<i class="parameter"><tt>lock</tt></i> (neither for reading nor writing). This lock has to be unlocked
by <a href="glib-Threads.html#g-static-rw-lock-writer-unlock"><tt class="function">g_static_rw_lock_writer_unlock()</tt></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to lock for writing.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033259"></a><h3><a name="g-static-rw-lock-writer-trylock"></a>g_static_rw_lock_writer_trylock ()</h3><a class="indexterm" name="id3033271"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_static_rw_lock_writer_trylock (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Tries to lock <i class="parameter"><tt>lock</tt></i> for writing. If <i class="parameter"><tt>lock</tt></i> is already locked (for
either reading or writing) by another thread, it immediately returns
<tt class="literal">FALSE</tt>. Otherwise it locks <i class="parameter"><tt>lock</tt></i> for writing and returns <tt class="literal">TRUE</tt>. This
lock has to be unlocked by <a href="glib-Threads.html#g-static-rw-lock-writer-unlock"><tt class="function">g_static_rw_lock_writer_unlock()</tt></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to lock for writing.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if <i class="parameter"><tt>lock</tt></i> could be locked for writing.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033392"></a><h3><a name="g-static-rw-lock-writer-unlock"></a>g_static_rw_lock_writer_unlock ()</h3><a class="indexterm" name="id3033404"></a><pre class="programlisting">void        g_static_rw_lock_writer_unlock  (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Unlocks <i class="parameter"><tt>lock</tt></i>. If a thread waits to lock <i class="parameter"><tt>lock</tt></i> for writing and all
locks for reading have been unlocked, the waiting thread is woken up
and can lock <i class="parameter"><tt>lock</tt></i> for writing. If no thread waits to lock <i class="parameter"><tt>lock</tt></i> for
writing and threads wait to lock <i class="parameter"><tt>lock</tt></i> for reading, the waiting
threads are woken up and can lock <i class="parameter"><tt>lock</tt></i> for reading.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to unlock after writing.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033500"></a><h3><a name="g-static-rw-lock-free"></a>g_static_rw_lock_free ()</h3><a class="indexterm" name="id3033511"></a><pre class="programlisting">void        g_static_rw_lock_free           (<a href="glib-Threads.html#GStaticRWLock">GStaticRWLock</a> *lock);</pre><p>
Releases all resources allocated to <i class="parameter"><tt>lock</tt></i>. 
</p><p>
You don't have to call this functions for a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> with an
unbounded lifetime, i.e. objects declared 'static', but if you have a
<a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> as a member of a structure and the structure is freed,
you should also free the <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>lock</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticRWLock"><span class="type">GStaticRWLock</span></a> to be freed.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033604"></a><h3><a name="GCond"></a>struct GCond</h3><a class="indexterm" name="id3033615"></a><pre class="programlisting">struct GCond;</pre><p>
The <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a> struct is an opaque data structure to represent a
condition. A <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a> is an object, that threads can block on, if they
find a certain condition to be false. If other threads change the
state of this condition they can signal the <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>, such that the
waiting thread is woken up. 
</p><p>
</p><div class="example"><a name="id3033660"></a><p class="title"><b>Example 9. Using GCond to block a thread until a condition is satisfied</b></p><pre class="programlisting">
GCond* data_cond = NULL;   /* Must be initialized somewhere */
GMutex* data_mutex = NULL; /* Must be initialized somewhere */
gpointer current_data = NULL;

void push_data (gpointer data)
{
  g_mutex_lock (data_mutex);
  current_data = data;
  g_cond_signal (data_cond);
  g_mutex_unlock (data_mutex);
}

gpointer pop_data ()
{
  gpointer data;

  g_mutex_lock (data_mutex);
  while (!current_data)
      g_cond_wait (data_cond, data_mutex);
  data = current_data;
  current_data = NULL;
  g_mutex_unlock (data_mutex);
  return data;
}
</pre></div><p>
</p><p>
Whenever a thread calls <tt class="function"><tt class="function">pop_data()</tt></tt> now, it will 
wait until current_data is non-<tt class="literal">NULL</tt>, i.e. until some other thread 
has called <tt class="function"><tt class="function">push_data()</tt></tt>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
It is important to use the <a href="glib-Threads.html#g-cond-wait"><tt class="function">g_cond_wait()</tt></a> and <a href="glib-Threads.html#g-cond-timed-wait"><tt class="function">g_cond_timed_wait()</tt></a>
functions only inside a loop, which checks for the condition to be
true as it is not guaranteed that the waiting thread will find it
fulfilled, even if the signaling thread left the condition
in that state. This is because another thread can have altered the
condition, before the waiting thread got the chance to be woken up,
even if the condition itself is protected by a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, like above.
</p></div><p>
A <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a> should only be accessed via the following functions.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
All of the <tt class="function">g_cond_*</tt> functions are actually macros. 
Apart from taking their addresses, you can however use them as if they 
were functions.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3033796"></a><h3><a name="g-cond-new"></a>g_cond_new ()</h3><a class="indexterm" name="id3033806"></a><pre class="programlisting"><a href="glib-Threads.html#GCond">GCond</a>*      g_cond_new                      ();</pre><p>
Creates a new <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>. This function will abort, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>
has not been called yet.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a new <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033869"></a><h3><a name="g-cond-signal"></a>g_cond_signal ()</h3><a class="indexterm" name="id3033880"></a><pre class="programlisting">void        g_cond_signal                   (<a href="glib-Threads.html#GCond">GCond</a> *cond);</pre><p>
If threads are waiting for <i class="parameter"><tt>cond</tt></i>, exactly one of them is woken up. It
is good practice to hold the same lock as the waiting thread, while
calling this function, though not required.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has
not yet been called and will do nothing then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>cond</tt></i> :</span></td><td>a <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3033959"></a><h3><a name="g-cond-broadcast"></a>g_cond_broadcast ()</h3><a class="indexterm" name="id3033970"></a><pre class="programlisting">void        g_cond_broadcast                (<a href="glib-Threads.html#GCond">GCond</a> *cond);</pre><p>
If threads are waiting for <i class="parameter"><tt>cond</tt></i>, all of them are woken up. It is good
practice to lock the same mutex as the waiting threads, while calling
this function, though not required.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has
not yet been called and will do nothing then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>cond</tt></i> :</span></td><td>a <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034048"></a><h3><a name="g-cond-wait"></a>g_cond_wait ()</h3><a class="indexterm" name="id3034059"></a><pre class="programlisting">void        g_cond_wait                     (<a href="glib-Threads.html#GCond">GCond</a> *cond,
                                             <a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>
Waits until this thread is woken up on <i class="parameter"><tt>cond</tt></i>. The <i class="parameter"><tt>mutex</tt></i> is unlocked
before falling asleep and locked again before resuming.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will immediately return then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>cond</tt></i> :</span></td><td>a <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, that is currently locked.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034171"></a><h3><a name="g-cond-timed-wait"></a>g_cond_timed_wait ()</h3><a class="indexterm" name="id3034183"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_cond_timed_wait               (<a href="glib-Threads.html#GCond">GCond</a> *cond,
                                             <a href="glib-Threads.html#GMutex">GMutex</a> *mutex,
                                             <a href="glib-Date-and-Time-Functions.html#GTimeVal">GTimeVal</a> *abs_time);</pre><p>
Waits until this thread is woken up on <i class="parameter"><tt>cond</tt></i>, but not longer than
until the time, that is specified by <i class="parameter"><tt>abs_time</tt></i>. The <i class="parameter"><tt>mutex</tt></i> is
unlocked before falling asleep and locked again before resuming.
</p><p>
If <i class="parameter"><tt>abs_time</tt></i> is <tt class="literal">NULL</tt>, <a href="glib-Threads.html#g-cond-timed-wait"><tt class="function">g_cond_timed_wait()</tt></a> acts like <a href="glib-Threads.html#g-cond-wait"><tt class="function">g_cond_wait()</tt></a>.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will immediately return <tt class="literal">TRUE</tt> then.
</p><p>
To easily calculate <i class="parameter"><tt>abs_time</tt></i> a combination of <a href="glib-Date-and-Time-Functions.html#g-get-current-time"><tt class="function">g_get_current_time()</tt></a>
and <a href="glib-Date-and-Time-Functions.html#g-time-val-add"><tt class="function">g_time_val_add()</tt></a> can be used.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>cond</tt></i> :</span></td><td>a <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>mutex</tt></i> :</span></td><td>a <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a>, that is currently locked.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>abs_time</tt></i> :</span></td><td>a <a href="glib-Date-and-Time-Functions.html#GTimeVal"><span class="type">GTimeVal</span></a>, determining the final time.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt>, if the thread is woken up in time.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034422"></a><h3><a name="g-cond-free"></a>g_cond_free ()</h3><a class="indexterm" name="id3034432"></a><pre class="programlisting">void        g_cond_free                     (<a href="glib-Threads.html#GCond">GCond</a> *cond);</pre><p>
Destroys the <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>cond</tt></i> :</span></td><td>a <a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034494"></a><h3><a name="GPrivate"></a>struct GPrivate</h3><a class="indexterm" name="id3034505"></a><pre class="programlisting">struct GPrivate;</pre><p>
The <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a> struct is an opaque data structure to represent a thread
private data key. Threads can thereby obtain and set a pointer, which
is private to the current thread. 
Take our <tt class="function"><tt class="function">give_me_next_number()</tt></tt> example from above.  
Now we don't want <tt class="literal">current_number</tt> to be shared
between the threads, but to be private to each thread. This can be
done as follows:

</p><div class="example"><a name="id3034552"></a><p class="title"><b>Example 10. Using GPrivate for per-thread data</b></p><pre class="programlisting">
  GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
                                       /* with g_private_new (g_free); */

  int give_me_next_number ()
  {
    int *current_number = g_private_get (current_number_key);

    if (!current_number)
    {
      current_number = g_new (int,1);
      *current_number = 0;
      g_private_set (current_number_key, current_number);
    }
    *current_number = calc_next_number (*current_number); 
    return *current_number;
  }
</pre></div><p>
</p><p>
Here the pointer belonging to the key <tt class="literal">current_number_key</tt> 
is read. If it is <tt class="literal">NULL</tt>, it has not been set yet. Then get memory for an 
integer value, assign this memory to the pointer and write the pointer
back. Now we have an integer value, that is private to the current thread.
</p><p>
The <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a> struct should only be accessed via the following functions.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
All of the <tt class="function">g_private_*</tt> functions are actually macros. 
Apart from taking their addresses, you can however use them as if they were 
functions.
</p></div></div><hr><div class="refsect2" lang="en"><a name="id3034623"></a><h3><a name="g-private-new"></a>g_private_new ()</h3><a class="indexterm" name="id3034634"></a><pre class="programlisting"><a href="glib-Threads.html#GPrivate">GPrivate</a>*   g_private_new                   (<a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> destructor);</pre><p>
Creates a new <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>. If <i class="parameter"><tt>destructor</tt></i> is non-<tt class="literal">NULL</tt>, it is a pointer
to a destructor function. Whenever a thread ends and the corresponding
pointer keyed to this instance of <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a> is non-<tt class="literal">NULL</tt>, the
destructor is called with this pointer as the argument.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
<i class="parameter"><tt>destructor</tt></i> is working quite differently from <i class="parameter"><tt>notify</tt></i> in
<a href="glib-Threads.html#g-static-private-set"><tt class="function">g_static_private_set()</tt></a>.
</p></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
A <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a> can not be freed. Reuse it instead, if you can to avoid
shortage or use <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>.
</p></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
This function will abort, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not been called yet.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>destructor</tt></i> :</span></td><td>a function to handle the data keyed to <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>, when a
thread ends.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a new <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034816"></a><h3><a name="g-private-get"></a>g_private_get ()</h3><a class="indexterm" name="id3034827"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_private_get                   (<a href="glib-Threads.html#GPrivate">GPrivate</a> *private_key);</pre><p>
Returns the pointer keyed to <i class="parameter"><tt>private_key</tt></i> for the current thread. This
pointer is <tt class="literal">NULL</tt>, when <a href="glib-Threads.html#g-private-set"><tt class="function">g_private_set()</tt></a> hasn't been called for the
current <i class="parameter"><tt>private_key</tt></i> and thread yet.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will return the value of <i class="parameter"><tt>private_key</tt></i> casted to <a href="glib-Basic-Types.html#gpointer"><span class="type">gpointer</span></a> then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the corresponding pointer.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3034952"></a><h3><a name="g-private-set"></a>g_private_set ()</h3><a class="indexterm" name="id3034963"></a><pre class="programlisting">void        g_private_set                   (<a href="glib-Threads.html#GPrivate">GPrivate</a> *private_key,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data);</pre><p>
Sets the pointer keyed to <i class="parameter"><tt>private_key</tt></i> for the current thread.
</p><p>
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called and will set <i class="parameter"><tt>private_key</tt></i> to <i class="parameter"><tt>data</tt></i> casted to <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>* then.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>data</tt></i> :</span></td><td>the new pointer.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3035080"></a><h3><a name="GStaticPrivate"></a>struct GStaticPrivate</h3><a class="indexterm" name="id3035092"></a><pre class="programlisting">struct GStaticPrivate {

};
</pre><p>
A <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> works almost like a <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>, but it has one
significant advantage. It doesn't need to be created at run-time like
a <a href="glib-Threads.html#GPrivate"><span class="type">GPrivate</span></a>, but can be defined at compile-time. This is similar to
the difference between <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> and <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. Now look at our
<tt class="function"><tt class="function">give_me_next_number()</tt></tt> example with <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>:
</p><p>
</p><div class="example"><a name="id3035175"></a><p class="title"><b>Example 11. Using GStaticPrivate for per-thread data</b></p><pre class="programlisting">
  int give_me_next_number ()
  {
    static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
    int *current_number = g_static_private_get (&amp;current_number_key);

    if (!current_number)
    {
      current_number = g_new (int,1);
      *current_number = 0;
      g_static_private_set (&amp;current_number_key, current_number, g_free);
    }
    *current_number = calc_next_number (*current_number); 
    return *current_number;
  }
</pre></div><p>
</p></div><hr><div class="refsect2" lang="en"><a name="id3035206"></a><h3><a name="G-STATIC-PRIVATE-INIT-CAPS"></a>G_STATIC_PRIVATE_INIT</h3><a class="indexterm" name="id3035216"></a><pre class="programlisting">#define G_STATIC_PRIVATE_INIT 
</pre><p>
Every <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> must be initialized with this macro, before it can
be used.
</p><p>
</p><div class="informalexample"><pre class="programlisting">
GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
</pre></div><p>
</p></div><hr><div class="refsect2" lang="en"><a name="id3035254"></a><h3><a name="g-static-private-init"></a>g_static_private_init ()</h3><a class="indexterm" name="id3035266"></a><pre class="programlisting">void        g_static_private_init           (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);</pre><p>
Initializes <i class="parameter"><tt>private_key</tt></i>. Alternatively you can initialize it with
<a href="glib-Threads.html#G-STATIC-PRIVATE-INIT-CAPS"><span class="type">G_STATIC_PRIVATE_INIT</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> to be initialized.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3035337"></a><h3><a name="g-static-private-get"></a>g_static_private_get ()</h3><a class="indexterm" name="id3035348"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_static_private_get            (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);</pre><p>
Works like <a href="glib-Threads.html#g-private-get"><tt class="function">g_private_get()</tt></a> only for a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>.
</p><p>
This function also works, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been called.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the corresponding pointer.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3035448"></a><h3><a name="g-static-private-set"></a>g_static_private_set ()</h3><a class="indexterm" name="id3035459"></a><pre class="programlisting">void        g_static_private_set            (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key,
                                             <a href="glib-Basic-Types.html#gpointer">gpointer</a> data,
                                             <a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);</pre><p>
Sets the pointer keyed to <i class="parameter"><tt>private_key</tt></i> for the current thread and the
function <i class="parameter"><tt>notify</tt></i> to be called with that pointer (<tt class="literal">NULL</tt> or non-<tt class="literal">NULL</tt>),
whenever the pointer is set again or whenever the current thread ends.
</p><p>
This function also works, if <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> has not yet been
called. If <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a> is called later, the <i class="parameter"><tt>data</tt></i> keyed to
<i class="parameter"><tt>private_key</tt></i> will be inherited only by the main thread, i.e. the one that
called <a href="glib-Threads.html#g-thread-init"><tt class="function">g_thread_init()</tt></a>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
<i class="parameter"><tt>notify</tt></i> is working quite differently from <i class="parameter"><tt>destructor</tt></i> in
<a href="glib-Threads.html#g-private-new"><tt class="function">g_private_new()</tt></a>.
</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>data</tt></i> :</span></td><td>the new pointer.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>notify</tt></i> :</span></td><td>a function to be called with the pointer, whenever the
current thread ends or sets this pointer again.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3035664"></a><h3><a name="g-static-private-free"></a>g_static_private_free ()</h3><a class="indexterm" name="id3035675"></a><pre class="programlisting">void        g_static_private_free           (<a href="glib-Threads.html#GStaticPrivate">GStaticPrivate</a> *private_key);</pre><p>
Releases all resources allocated to <i class="parameter"><tt>private_key</tt></i>. 
</p><p>
You don't have to call this functions for a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> with an
unbounded lifetime, i.e. objects declared 'static', but if you have a
<a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> as a member of a structure and the structure is freed,
you should also free the <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>private_key</tt></i> :</span></td><td>a <a href="glib-Threads.html#GStaticPrivate"><span class="type">GStaticPrivate</span></a> to be freed.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3035768"></a><h3><a name="GOnce"></a>struct GOnce</h3><a class="indexterm" name="id3035779"></a><pre class="programlisting">struct GOnce {

  volatile GOnceStatus status;
  volatile gpointer retval;
};
</pre><p>
A <span class="structname">GOnce</span> struct controls a one-time initialization function. 
Any one-time initialization function must have its own unique <span class="structname">GOnce</span> 
struct.
</p><p>Since 2.4

</p></div><hr><div class="refsect2" lang="en"><a name="id3035809"></a><h3><a name="GOnceStatus"></a>enum GOnceStatus</h3><a class="indexterm" name="id3035820"></a><pre class="programlisting">typedef enum
{
  G_ONCE_STATUS_NOTCALLED,
  G_ONCE_STATUS_PROGRESS,
  G_ONCE_STATUS_READY  
} GOnceStatus;
</pre><p>
The possible stati of a one-time initialization function controlled by a <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> struct.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><tt class="literal">G_ONCE_STATUS_NOTCALLED</tt></span></td><td>the function has not been called yet.
</td></tr><tr><td><span class="term"><tt class="literal">G_ONCE_STATUS_PROGRESS</tt></span></td><td>the function call is currently in progress.
</td></tr><tr><td><span class="term"><tt class="literal">G_ONCE_STATUS_READY</tt></span></td><td>the function has been called.
</td></tr></tbody></table></div><p>Since 2.4

</p></div><hr><div class="refsect2" lang="en"><a name="id3035900"></a><h3><a name="G-ONCE-INIT-CAPS"></a>G_ONCE_INIT</h3><a class="indexterm" name="id3035911"></a><pre class="programlisting">#define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
</pre><p>
A <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> must be initialized with this macro, before it can be used. 
</p><p>
</p><div class="informalexample"><pre class="programlisting">
GOnce my_once = G_ONCE_INIT;
</pre></div><p>
</p><p>Since 2.4


</p></div><hr><div class="refsect2" lang="en"><a name="id3035952"></a><h3><a name="g-once"></a>g_once()</h3><a class="indexterm" name="id3035963"></a><pre class="programlisting">#define     g_once(once, func, arg)</pre><p>
The first call to this routine by a process with a given <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> struct calls <i class="parameter"><tt>func</tt></i> with the given 
argument. Thereafter, subsequent calls to <a href="glib-Threads.html#g-once"><tt class="function">g_once()</tt></a>  with the same <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> struct do not call <i class="parameter"><tt>func</tt></i> 
again, but return the stored result of the first call. On return from <a href="glib-Threads.html#g-once"><tt class="function">g_once()</tt></a>, the status of <i class="parameter"><tt>once</tt></i>
will be <tt class="literal">G_ONCE_STATUS_READY</tt>.
</p><p>
For example, a mutex or a thread-specific data key must be created exactly once. In a threaded 
environment, calling <a href="glib-Threads.html#g-once"><tt class="function">g_once()</tt></a> ensures that the initialization is serialized across multiple threads.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Calling <a href="glib-Threads.html#g-once"><tt class="function">g_once()</tt></a> recursively on the same <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> struct in <i class="parameter"><tt>func</tt></i> will lead to a deadlock.
</p></div><p>
</p><div class="informalexample"><pre class="programlisting">
gpointer 
<tt class="function">get_debug_flags()</tt>
{
  static GOnce my_once = G_ONCE_INIT;
  
  g_once (&amp;my_once, parse_debug_flags, NULL);

  return my_once.retval;
}
</pre></div><p>
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>once</tt></i> :</span></td><td>a <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> structure
</td></tr><tr><td><span class="term"><i class="parameter"><tt>func</tt></i> :</span></td><td>the function associated to <i class="parameter"><tt>once</tt></i>. This function is called only once, regardless of the 
       number of times it and its associated <a href="glib-Threads.html#GOnce"><span class="type">GOnce</span></a> struct are passed to <a href="glib-Threads.html#g-once"><tt class="function">g_once()</tt></a> .
</td></tr><tr><td><span class="term"><i class="parameter"><tt>arg</tt></i> :</span></td><td> data to be passed to <i class="parameter"><tt>func</tt></i>
</td></tr></tbody></table></div><p>Since 2.4


</p></div></div><div class="refsect1" lang="en"><a name="id3036208"></a><h2>See Also</h2><p>
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><a href="glib-Thread-Pools.html#GThreadPool"><span class="type">GThreadPool</span></a></span></td><td><p>Thread pools.</p></td></tr><tr><td><span class="term"><a href="glib-Asynchronous-Queues.html#GAsyncQueue"><span class="type">GAsyncQueue</span></a></span></td><td><p>Send asynchronous messages between threads.</p></td></tr></tbody></table></div><p>
</p></div></div><table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle"><td align="left"><a accesskey="p" href="glib-The-Main-Event-Loop.html"><b>&lt;&lt; The Main Event Loop</b></a></td><td align="right"><a accesskey="n" href="glib-Thread-Pools.html"><b>Thread Pools &gt;&gt;</b></a></td></tr></table></body></html>