📄 glib-thread-pools.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Thread Pools</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><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-Threads.html" title="Threads"><link rel="next" href="glib-Asynchronous-Queues.html" title="Asynchronous Queues"><meta name="generator" content="GTK-Doc V1.9 (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"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Threads.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-Asynchronous-Queues.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr><tr><td colspan="5" class="shortcuts"><nobr><a href="#id2904507" class="shortcut">Top</a>  |  <a href="#id2904921" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-Thread-Pools"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id2904507"></a><span class="refentrytitle">Thread Pools</span></h2><p>Thread Pools — pools of threads to execute work concurrently</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a>;<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a>* <a class="link" href="glib-Thread-Pools.html#g-thread-pool-new">g_thread_pool_new</a> (<a class="link" href="glib-Doubly-Linked-Lists.html#GFunc">GFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data, <a class="link" href="glib-Basic-Types.html#gint">gint</a> max_threads, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> exclusive, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-push">g_thread_pool_push</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-max-threads">g_thread_pool_set_max_threads</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool, <a class="link" href="glib-Basic-Types.html#gint">gint</a> max_threads, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-max-threads">g_thread_pool_get_max_threads</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-num-threads">g_thread_pool_get_num_threads</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-unprocessed">g_thread_pool_unprocessed</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-free">g_thread_pool_free</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> immediate, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> wait_);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-max-unused-threads">g_thread_pool_set_max_unused_threads</a> (<a class="link" href="glib-Basic-Types.html#gint">gint</a> max_threads);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-max-unused-threads">g_thread_pool_get_max_unused_threads</a> (void);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-num-unused-threads">g_thread_pool_get_num_unused_threads</a> (void);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-stop-unused-threads">g_thread_pool_stop_unused_threads</a> (void);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-sort-function">g_thread_pool_set_sort_function</a> (<a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a> *pool, <a class="link" href="glib-Doubly-Linked-Lists.html#GCompareDataFunc">GCompareDataFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);void <a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-max-idle-time">g_thread_pool_set_max_idle_time</a> (<a class="link" href="glib-Basic-Types.html#guint">guint</a> interval);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-max-idle-time">g_thread_pool_get_max_idle_time</a> (void);</pre></div><div class="refsect1" lang="en"><a name="id2904921"></a><h2>Description</h2><p>Sometimes you wish to asynchronously fork out the execution of work andcontinue working in your own thread. If that will happen often, theoverhead of starting and destroying a thread each time might be toohigh. In such cases reusing already started threads seems like a goodidea. And it indeed is, but implementing this can be tedious anderror-prone.</p><p>Therefore GLib provides thread pools for your convenience. An addedadvantage is, that the threads can be shared between the differentsubsystems of your program, when they are using GLib.</p><p>To create a new thread pool, you use <a class="link" href="glib-Thread-Pools.html#g-thread-pool-new"><code class="function">g_thread_pool_new()</code></a>. It isdestroyed by <a class="link" href="glib-Thread-Pools.html#g-thread-pool-free"><code class="function">g_thread_pool_free()</code></a>.</p><p>If you want to execute a certain task within a thread pool, you call<a class="link" href="glib-Thread-Pools.html#g-thread-pool-push"><code class="function">g_thread_pool_push()</code></a>.</p><p>To get the current number of running threads you call<a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-num-threads"><code class="function">g_thread_pool_get_num_threads()</code></a>. To get the number of stillunprocessed tasks you call <a class="link" href="glib-Thread-Pools.html#g-thread-pool-unprocessed"><code class="function">g_thread_pool_unprocessed()</code></a>. To control themaximal number of threads for a thread pool, you use<a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-max-threads"><code class="function">g_thread_pool_get_max_threads()</code></a> and <a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-max-threads"><code class="function">g_thread_pool_set_max_threads()</code></a>.</p><p>Finally you can control the number of unused threads, that are keptalive by GLib for future use. The current number can be fetched with<a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-num-unused-threads"><code class="function">g_thread_pool_get_num_unused_threads()</code></a>. The maximal number can becontrolled by <a class="link" href="glib-Thread-Pools.html#g-thread-pool-get-max-unused-threads"><code class="function">g_thread_pool_get_max_unused_threads()</code></a> and<a class="link" href="glib-Thread-Pools.html#g-thread-pool-set-max-unused-threads"><code class="function">g_thread_pool_set_max_unused_threads()</code></a>. All currently unused threadscan be stopped by calling <a class="link" href="glib-Thread-Pools.html#g-thread-pool-stop-unused-threads"><code class="function">g_thread_pool_stop_unused_threads()</code></a>.</p></div><div class="refsect1" lang="en"><a name="id2905094"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2905104"></a><h3><a name="GThreadPool"></a>GThreadPool</h3><a class="indexterm" name="id2905116"></a><pre class="programlisting">typedef struct { GFunc func; gpointer user_data; gboolean exclusive;} GThreadPool;</pre><p>The <a class="link" href="glib-Thread-Pools.html#GThreadPool"><span class="type">GThreadPool</span></a> struct represents a thread pool. It has three publicread-only members, but the underlying struct is bigger, so you must not copy this struct.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><a class="link" href="glib-Doubly-Linked-Lists.html#GFunc">GFunc</a> <em class="structfield"><code>func</code></em>;</span></p></td><td>the function to execute in the threads of this pool</td></tr><tr><td><p><span class="term"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> <em class="structfield"><code>user_data</code></em>;</span></p></td><td>the user data for the threads of this pool</td></tr><tr><td><p><span class="term"><a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <em class="structfield"><code>exclusive</code></em>;</span></p></td><td>are all threads exclusive to this pool</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2905210"></a><h3><a name="g-thread-pool-new"></a>g_thread_pool_new ()</h3><a class="indexterm" name="id2905224"></a><pre class="programlisting"><a class="link" href="glib-Thread-Pools.html#GThreadPool">GThreadPool</a>* g_thread_pool_new (<a class="link" href="glib-Doubly-Linked-Lists.html#GFunc">GFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data, <a class="link" href="glib-Basic-Types.html#gint">gint</a> max_threads, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> exclusive, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);</pre><p>This function creates a new thread pool.</p><p>Whenever you call <a class="link" href="glib-Thread-Pools.html#g-thread-pool-push"><code class="function">g_thread_pool_push()</code></a>, either a new thread iscreated or an unused one is reused. At most <em class="parameter"><code>max_threads</code></em> threadsare running concurrently for this thread pool. <em class="parameter"><code>max_threads</code></em> = -1allows unlimited threads to be created for this thread pool. Thenewly created or reused thread now executes the function <em class="parameter"><code>func</code></em> withthe two arguments. The first one is the parameter to<a class="link" href="glib-Thread-Pools.html#g-thread-pool-push"><code class="function">g_thread_pool_push()</code></a> and the second one is <em class="parameter"><code>user_data</code></em>.</p><p>The parameter <em class="parameter"><code>exclusive</code></em> determines, whether the thread pool ownsall threads exclusive or whether the threads are sharedglobally. If <em class="parameter"><code>exclusive</code></em> is <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a>, <em class="parameter"><code>max_threads</code></em> threads are startedimmediately and they will run exclusively for this thread pool untilit is destroyed by <a class="link" href="glib-Thread-Pools.html#g-thread-pool-free"><code class="function">g_thread_pool_free()</code></a>. If <em class="parameter"><code>exclusive</code></em> is <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS"><code class="literal">FALSE</code></a>,threads are created, when needed and shared between allnon-exclusive thread pools. This implies that <em class="parameter"><code>max_threads</code></em> may notbe -1 for exclusive thread pools.</p><p><em class="parameter"><code>error</code></em> can be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> to ignore errors, or non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> to reporterrors. An error can only occur when <em class="parameter"><code>exclusive</code></em> is set to <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a> andnot all <em class="parameter"><code>max_threads</code></em> threads could be created.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>func</code></em> :</span></p></td><td> a function to execute in the threads of the new thread pool</td></tr><tr><td><p><span class="term"><em class="parameter"><code>user_data</code></em> :</span></p></td><td> user data that is handed over to <em class="parameter"><code>func</code></em> every time it is called</td></tr><tr><td><p><span class="term"><em class="parameter"><code>max_threads</code></em> :</span></p></td><td> the maximal number of threads to execute concurrently in the new thread pool, -1 means no limit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -