Synopsis
#include <glib.h>
GThreadPool;
GThreadPool* g_thread_pool_new (GFunc func,
gpointer user_data,
gint max_threads,
gboolean exclusive,
GError **error);
void g_thread_pool_push (GThreadPool *pool,
gpointer data,
GError **error);
void g_thread_pool_set_max_threads (GThreadPool *pool,
gint max_threads,
GError **error);
gint g_thread_pool_get_max_threads (GThreadPool *pool);
guint g_thread_pool_get_num_threads (GThreadPool *pool);
guint g_thread_pool_unprocessed (GThreadPool *pool);
void g_thread_pool_free (GThreadPool *pool,
gboolean immediate,
gboolean wait_);
void g_thread_pool_set_max_unused_threads
(gint max_threads);
gint g_thread_pool_get_max_unused_threads
(void);
guint g_thread_pool_get_num_unused_threads
(void);
void g_thread_pool_stop_unused_threads (void);
void g_thread_pool_set_sort_function (GThreadPool *pool,
GCompareDataFunc func,
gpointer user_data);
void g_thread_pool_set_max_idle_time (guint interval);
guint g_thread_pool_get_max_idle_time (void);
DescriptionSometimes you wish to asynchronously fork out the execution of work and continue working in your own thread. If that will happen often, the overhead of starting and destroying a thread each time might be too high. In such cases reusing already started threads seems like a good idea. And it indeed is, but implementing this can be tedious and error-prone. Therefore GLib provides thread pools for your convenience. An added advantage is, that the threads can be shared between the different subsystems of your program, when they are using GLib.
To create a new thread pool, you use
If you want to execute a certain task within a thread pool, you call
To get the current number of running threads you call
Finally you can control the number of unused threads, that are kept
alive by GLib for future use. The current number can be fetched with
DetailsGThreadPooltypedef struct {
GFunc func;
gpointer user_data;
gboolean exclusive;
} GThreadPool;
The GThreadPool struct represents a thread pool. It has three public read-only members, but the underlying struct is bigger, so you must not copy this struct. g_thread_pool_new ()GThreadPool* g_thread_pool_new (GFunc func, gpointer user_data, gint max_threads, gboolean exclusive, GError **error); This function creates a new thread pool.
Whenever you call
The parameter
g_thread_pool_push ()void g_thread_pool_push (GThreadPool *pool, gpointer data, GError **error);
Inserts
g_thread_pool_set_max_threads ()void g_thread_pool_set_max_threads (GThreadPool *pool, gint max_threads, GError **error);
Sets the maximal allowed number of threads for
Setting
A thread is never terminated while calling
g_thread_pool_get_max_threads ()gint g_thread_pool_get_max_threads (GThreadPool *pool);
Returns the maximal number of threads for
g_thread_pool_get_num_threads ()guint g_thread_pool_get_num_threads (GThreadPool *pool);
Returns the number of threads currently running in
g_thread_pool_unprocessed ()guint g_thread_pool_unprocessed (GThreadPool *pool);
Returns the number of tasks still unprocessed in
g_thread_pool_free ()void g_thread_pool_free (GThreadPool *pool, gboolean immediate, gboolean wait_);
Frees all resources allocated for
If
If
After calling this function
g_thread_pool_set_max_unused_threads ()void g_thread_pool_set_max_unused_threads
(gint max_threads);
Sets the maximal number of unused threads to
g_thread_pool_get_max_unused_threads ()gint g_thread_pool_get_max_unused_threads (void); Returns the maximal allowed number of unused threads.
g_thread_pool_get_num_unused_threads ()guint g_thread_pool_get_num_unused_threads (void); Returns the number of currently unused threads.
g_thread_pool_stop_unused_threads ()void g_thread_pool_stop_unused_threads (void);
Stops all currently unused threads. This does not change the
maximal number of unused threads. This function can be used to
regularly stop all unused threads e.g. from
g_thread_pool_set_sort_function ()void g_thread_pool_set_sort_function (GThreadPool *pool, GCompareDataFunc func, gpointer user_data);
Sets the function used to sort the list of tasks. This allows the
tasks to be processed by a priority determined by Note, if the maximum number of threads is more than 1, the order that threads are executed can not be guranteed 100%. Threads are scheduled by the operating system and are executed at random. It cannot be assumed that threads are executed in the order they are created.
Since 2.10 g_thread_pool_set_max_idle_time ()void g_thread_pool_set_max_idle_time (guint interval);
This function will set the maximum
By setting
Since 2.10 g_thread_pool_get_max_idle_time ()guint g_thread_pool_get_max_idle_time (void);
This function will return the maximum If this function returns 0, threads waiting in the thread pool for new work are not stopped.
Since 2.10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||