📄 glib-threads.html
字号:
This function can also be used, if <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a> has not yet beencalled and will do nothing then.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>mutex</code></em> :</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="id2873490"></a><h3><a name="g-mutex-free"></a>g_mutex_free ()</h3><a class="indexterm" name="id2873501"></a><pre class="programlisting">void g_mutex_free (<a href="glib-Threads.html#GMutex">GMutex</a> *mutex);</pre><p>Destroys <em class="parameter"><code>mutex</code></em>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>mutex</code></em> :</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="id2873561"></a><h3><a name="GStaticMutex"></a>GStaticMutex</h3><a class="indexterm" name="id2873571"></a><pre class="programlisting">typedef struct _GStaticMutex 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 significantadvantage. 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 andsafer version of our <code class="function"><code class="function">give_me_next_number()</code></code> example:</p><p></p><div class="example"><a name="id2873632"></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 (&mutex); ret_val = current_number = calc_next_number (current_number); g_static_mutex_unlock (&mutex); return ret_val; }</pre></div><p></p><p>Sometimes you would like to dynamically create a mutex. If you don'twant to require prior calling to <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a>, because your codeshould also be usable in non-threaded programs, you are not able touse <a href="glib-Threads.html#g-mutex-new"><code class="function">g_mutex_new()</code></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"><code class="function">g_thread_init()</code></a>. In theses cases you can also use a <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>. Itmust be initialized with <a href="glib-Threads.html#g-static-mutex-init"><code class="function">g_static_mutex_init()</code></a> before using it andfreed with with <a href="glib-Threads.html#g-static-mutex-free"><code class="function">g_static_mutex_free()</code></a> when not needed anymore to freeup 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 withthe following functions, as it is defined differently on differentplatforms.</p><p>All of the <code class="function">g_static_mutex_*</code> functions can also be used, if <a href="glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></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 <code class="function">g_static_mutex_*</code> 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="id2873786"></a><h3><a name="G-STATIC-MUTEX-INIT:CAPS"></a>G_STATIC_MUTEX_INIT</h3><a class="indexterm" name="id2873798"></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 beused. This macro can used be to initialize a variable, but it cannotbe assigned to a variable. In that case you have to use<a href="glib-Threads.html#g-static-mutex-init"><code class="function">g_static_mutex_init()</code></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="id2873847"></a><h3><a name="g-static-mutex-init"></a>g_static_mutex_init ()</h3><a class="indexterm" name="id2873858"></a><pre class="programlisting">void g_static_mutex_init (<a href="glib-Threads.html#GStaticMutex">GStaticMutex</a> *mutex);</pre><p>Initializes <em class="parameter"><code>mutex</code></em>. 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"><em class="parameter"><code>mutex</code></em> :</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="id2873928"></a><h3><a name="g-static-mutex-lock"></a>g_static_mutex_lock ()</h3><a class="indexterm" name="id2873939"></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"><code class="function">g_mutex_lock()</code></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"><em class="parameter"><code>mutex</code></em> :</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="id2874013"></a><h3><a name="g-static-mutex-trylock"></a>g_static_mutex_trylock ()</h3><a class="indexterm" name="id2874024"></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"><code class="function">g_mutex_trylock()</code></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"><em class="parameter"><code>mutex</code></em> :</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><code class="literal">TRUE</code>, 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="id2874121"></a><h3><a name="g-static-mutex-unlock"></a>g_static_mutex_unlock ()</h3><a class="indexterm" name="id2874132"></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"><code class="function">g_mutex_unlock()</code></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"><em class="parameter"><code>mutex</code></em> :</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="id2874206"></a><h3><a name="g-static-mutex-get-mutex"></a>g_static_mutex_get_mutex ()</h3><a class="indexterm" name="id2874217"></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"><code class="function">g_cond_wait()</code></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 thecorresponding <a href="glib-Threads.html#GMutex"><span class="type">GMutex</span></a> for <em class="parameter"><code>mutex</code></em>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>mutex</code></em> :</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 <em class="parameter"><code>mutex</code></em>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2874338"></a><h3><a name="g-static-mutex-free"></a>g_static_mutex_free ()</h3><a class="indexterm" name="id2874349"></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 <em class="parameter"><code>mutex</code></em>. </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 anunbounded 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"><em class="parameter"><code>mutex</code></em> :</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="id2874442"></a><h3><a name="G-LOCK-DEFINE:CAPS"></a>G_LOCK_DEFINE()</h3><a class="indexterm" name="id2874452"></a><pre class="programlisting">#define G_LOCK_DEFINE(name)</pre><p>The <code class="literal">G_LOCK_</code>* macros provide a convenient interface to <a href="glib-Threads.html#GStaticMutex"><span class="type">GStaticMutex</span></a>with t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -