⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glib-threads.html

📁 glid编写实例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Threads</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-core.html" title="GLib Core Application Support"><link rel="prev" href="glib-The-Main-Event-Loop.html" title="The Main Event Loop"><link rel="next" href="glib-Thread-Pools.html" title="Thread Pools"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></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 class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Threads</span></h2><p>Threads &#8212; thread abstraction; including threads, different mutexes, conditionsand thread private data.</p></td><td valign="top" align="right"></td></tr></table></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>;            <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>;            <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);            <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);            <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)            <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);            <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);            <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);            <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);            <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);            <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="id2870542"></a><h2>Description</h2><p>Threads act almost like processes, but unlike processes all threads ofone process share the same memory. This is good, as it provides easycommunication between the involved threads via this shared memory, andit is bad, because strange things (so called Heisenbugs) might happen,when the program is not carefully designed. Especially bad is, that dueto the concurrent nature of threads no assumptions on the order ofexecution of different threads can be done unless explicitly forced bythe programmer through synchronization primitives.</p><p>The aim of the thread related functions in GLib is to provide aportable means for writing multi-threaded software. There areprimitives 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 allowsynchronization of threads (<a href="glib-Threads.html#GCond"><span class="type">GCond</span></a>) and finally there are primitivesfor 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 areprimitives 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="id2870644"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2870650"></a><h3><a name="G-THREADS-ENABLED:CAPS"></a>G_THREADS_ENABLED</h3><a class="indexterm" name="id2870661"></a><pre class="programlisting">#define G_THREADS_ENABLED</pre><p>This macro is defined, if GLib was compiled with thread support. Thisdoes not necessarily mean, that there is a thread implementationavailable, but the infrastructure is in place and once you provide athread implementation to <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a>, GLib will be multi-threadsafe. 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="id2870700"></a><h3><a name="G-THREADS-IMPL-POSIX:CAPS"></a>G_THREADS_IMPL_POSIX</h3><a class="indexterm" name="id2870711"></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="id2870728"></a><h3><a name="G-THREADS-IMPL-SOLARIS:CAPS"></a>G_THREADS_IMPL_SOLARIS</h3><a class="indexterm" name="id2870739"></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="id2870755"></a><h3><a name="G-THREADS-IMPL-NONE:CAPS"></a>G_THREADS_IMPL_NONE</h3><a class="indexterm" name="id2870767"></a><pre class="programlisting">#define G_THREADS_IMPL_NONE</pre><p>This macro is defined, if no thread implementation is used. You canhowever provide one to <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a> to make GLib multi-thread safe.</p></div><hr><div class="refsect2" lang="en"><a name="id2870795"></a><h3><a name="G-THREAD-ERROR:CAPS"></a>G_THREAD_ERROR</h3><a class="indexterm" name="id2870805"></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="id2870822"></a><h3><a name="GThreadError"></a>enum GThreadError</h3><a class="indexterm" name="id2870832"></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"><code class="literal">G_THREAD_ERROR_AGAIN</code></span></td><td>a thread couldn't be created due to resourceshortage. Try again later.</td></tr></tbody></table></div></div><hr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -