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

📄 glib-threads.html

📁 glid编写实例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<div class="refsect2" lang="en"><a name="id2870870"></a><h3><a name="GThreadFunctions"></a>GThreadFunctions</h3><a class="indexterm" name="id2870880"></a><pre class="programlisting">typedef struct {  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);} GThreadFunctions;</pre><p>This function table is used by <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a> to initialize thethread system. The functions in that table are directly used by theirg_* prepended counterparts, that are described here, e.g. if you call<a href="glib-Threads.html#g-mutex-new"><code class="function">g_mutex_new()</code></a> then <code class="function">mutex_new()</code> from the table provided to<a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></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="id2870985"></a><h3><a name="g-thread-init"></a>g_thread_init ()</h3><a class="indexterm" name="id2870996"></a><pre class="programlisting">void        g_thread_init                   (<a href="glib-Threads.html#GThreadFunctions">GThreadFunctions</a> *vtable);</pre><p>If you use GLib from more than one thread, you must initializethe thread system by calling <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a>. Most of the time you will only have to call <code class="literal">g_thread_init (NULL)</code>. </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"><code class="function">g_thread_init()</code></a> with a non-<code class="literal">NULL</code> parameter if youreally 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"><code class="function">g_thread_init()</code></a> must not be called directly or indirectly as acallback from GLib. Also no mutexes may be currently locked, whilecalling <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a>.</p></div><p><a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a> might only be called once. On the second callit will abort with an error. If you want to make sure, that the threadsystem 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 programwill 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 <em class="parameter"><code>vtable</code></em> is <code class="literal">NULL</code> or if not allelements of <em class="parameter"><code>vtable</code></em> are non-<code class="literal">NULL</code>, then <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></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"><code class="function">g_thread_init()</code></a> in your program, you have to link with thelibraries that the command <span><strong class="command">pkg-config --libs gthread-2.0</strong></span> outputs. This is not the case for all the other thread related functions ofGLib. 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"><em class="parameter"><code>vtable</code></em>&#160;:</span></td><td>a function table of type <a href="glib-Threads.html#GThreadFunctions"><span class="type">GThreadFunctions</span></a>, that provides theentry points to the thread system to be used.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2871246"></a><h3><a name="g-thread-supported"></a>g_thread_supported ()</h3><a class="indexterm" name="id2871258"></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 ornot.</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 ityou 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>&#160;:</span></td><td><code class="literal">TRUE</code>, if the thread system is initialized.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2871308"></a><h3><a name="GThreadFunc"></a>GThreadFunc ()</h3><a class="indexterm" name="id2871319"></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 <em class="parameter"><code>func</code></em> functions passed to<a href="glib-Threads.html#g-thread-create"><code class="function">g_thread_create()</code></a> or <a href="glib-Threads.html#g-thread-create-full"><code class="function">g_thread_create_full()</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>data</code></em>&#160;:</span></td><td>data passed to the thread.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>the return value of the thread, which will be returned by<a href="glib-Threads.html#g-thread-join"><code class="function">g_thread_join()</code></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2871415"></a><h3><a name="GThreadPriority"></a>enum GThreadPriority</h3><a class="indexterm" name="id2871426"></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 reallybehave accordingly. On some systems (e.g. Linux) there are no threadpriorities. On other systems (e.g. Solaris) there doesn't seem to bedifferent scheduling for different priorities. All in all try to avoidbeing dependent on priorities.</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><code class="literal">G_THREAD_PRIORITY_LOW</code></span></td><td>a priority lower than normal</td></tr><tr><td><span class="term"><code class="literal">G_THREAD_PRIORITY_NORMAL</code></span></td><td>the default priority</td></tr><tr><td><span class="term"><code class="literal">G_THREAD_PRIORITY_HIGH</code></span></td><td>a priority higher than normal</td></tr><tr><td><span class="term"><code class="literal">G_THREAD_PRIORITY_URGENT</code></span></td><td>the highest priority</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2871520"></a><h3><a name="GThread"></a>GThread</h3><a class="indexterm" name="id2871531"></a><pre class="programlisting">typedef 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 publicread-only members, but the underlying struct is bigger, so you mustnot 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"><code class="function">g_thread_join()</code></a> is called for that thread.</p></div></div><hr><div class="refsect2" lang="en"><a name="id2871575"></a><h3><a name="g-thread-create"></a>g_thread_create ()</h3><a class="indexterm" name="id2871586"></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>

⌨️ 快捷键说明

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