Struct
GLibThreadPool
Description [src]
struct GThreadPool {
GFunc func;
gpointer user_data;
gboolean exclusive;
}
The GThreadPool
struct represents a thread pool.
A thread pool is useful when 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 g_thread_pool_new()
.
It is destroyed by g_thread_pool_free()
.
If you want to execute a certain task within a thread pool, use g_thread_pool_push()
.
To get the current number of running threads you call g_thread_pool_get_num_threads()
.
To get the number of still unprocessed tasks you call g_thread_pool_unprocessed()
.
To control the maximum number of threads for a thread pool, you use
g_thread_pool_get_max_threads()
. and g_thread_pool_set_max_threads()
.
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 g_thread_pool_get_num_unused_threads()
.
The maximum number can be controlled by g_thread_pool_get_max_unused_threads()
and
g_thread_pool_set_max_unused_threads()
. All currently unused threads
can be stopped by calling g_thread_pool_stop_unused_threads()
.
Structure members
func
The function to execute in the threads of this pool.
user_data
The user data for the threads of this pool.
exclusive
Are all threads exclusive to this pool.
Functions
g_thread_pool_get_max_idle_time
This function will return the maximum interval
that a
thread will wait in the thread pool for new tasks before
being stopped.
since: 2.10
g_thread_pool_new_full
This function creates a new thread pool similar to g_thread_pool_new()
but allowing item_free_func
to be specified to free the data passed
to g_thread_pool_push()
in the case that the GThreadPool
is stopped
and freed before all tasks have been executed.
since: 2.70
g_thread_pool_set_max_idle_time
This function will set the maximum interval
that a thread
waiting in the pool for new tasks can be idle for before
being stopped. This function is similar to calling
g_thread_pool_stop_unused_threads()
on a regular timeout,
except this is done on a per thread basis.
since: 2.10
g_thread_pool_set_max_unused_threads
Sets the maximal number of unused threads to max_threads
.
If max_threads
is -1, no limit is imposed on the number
of unused threads.
g_thread_pool_stop_unused_threads
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_timeout_add().
Instance methods
g_thread_pool_move_to_front
Moves the item to the front of the queue of unprocessed items, so that it will be processed next.
since: 2.46
g_thread_pool_set_max_threads
Sets the maximal allowed number of threads for pool
.
A value of -1 means that the maximal number of threads
is unlimited. If pool
is an exclusive thread pool, setting
the maximal number of threads to -1 is not allowed.
g_thread_pool_set_sort_function
Sets the function used to sort the list of tasks. This allows the
tasks to be processed by a priority determined by func
, and not
just in the order in which they were added to the pool.
since: 2.10