Thread Pools: GLib Reference Manual

## Thread Pools Thread Pools — pools of threads to execute work concurrently |

## Functions

GThreadPool * | g_thread_pool_new ()
GThreadPool * | g_thread_pool_new_full ()
gboolean | g_thread_pool_push ()
gboolean | g_thread_pool_set_max_threads ()
gint | g_thread_pool_get_max_threads ()
guint | g_thread_pool_get_num_threads ()
guint | g_thread_pool_unprocessed ()
void | g_thread_pool_free ()
void | g_thread_pool_set_max_unused_threads ()
gint | g_thread_pool_get_max_unused_threads ()
guint | g_thread_pool_get_num_unused_threads ()
void | g_thread_pool_stop_unused_threads ()
void | g_thread_pool_set_sort_function ()
void | g_thread_pool_set_max_idle_time ()
guint | g_thread_pool_get_max_idle_time ()
gboolean | g_thread_pool_move_to_front ()

## Types and Values

struct | GThreadPool

## Includes

    #include <glib.h>

## Description

Sometimes 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, you call `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 maximal 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 maximal 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()`.

## Functions

### 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 `g_thread_pool_push()`, either a new thread is created or an unused one is reused. At most `max_threads` threads are running concurrently for this thread pool. `max_threads` = -1 allows unlimited threads to be created for this thread pool. The newly created or reused thread now executes the function `func` with the two arguments. The first one is the parameter to `g_thread_pool_push()` and the second one is `user_data` .

Pass `g_get_num_processors()` to `max_threads` to create as many threads as there are logical processors on the system. This will not pin each thread to a specific processor.

The parameter `exclusive` determines whether the thread pool owns all threads exclusive or shares them with other thread pools. If `exclusive` is `TRUE`, `max_threads` threads are started immediately and they will run exclusively for this thread pool until it is destroyed by `g_thread_pool_free()`. If `exclusive` is `FALSE`, threads are created when needed and shared between all non-exclusive thread pools. This implies that `max_threads` may not be -1 for exclusive thread pools. Besides, exclusive thread pools are not affected by `g_thread_pool_set_max_idle_time()` since their threads are never considered idle and returned to the global pool.

`error` can be `NULL` to ignore errors, or non-`NULL` to report errors. An error can only occur when `exclusive` is set to `TRUE` and not all `max_threads` threads could be created. See GThreadError for possible errors that may occur. Note, even in case of error a valid GThreadPool is returned.

#### Parameters

func | a function to execute in the threads of the new thread pool |
user_data | user data that is handed over to `func` every time it is called |
max_threads | the maximal number of threads to execute concurrently in the new thread pool, -1 means no limit |
exclusive | should this thread pool be exclusive? |
error | return location for error, or `NULL` |

#### Returns

the new GThreadPool

### g_thread_pool_new_full ()

    GThreadPool *
    g_thread_pool_new_full (GFunc func,
                            gpointer user_data,
                            GDestroyNotify item_free_func,
                            gint max_threads,
                            gboolean exclusive,
                            GError **error);

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.

#### Parameters

func | a function to execute in the threads of the new thread pool |
user_data | user data that is handed over to `func` every time it is called |
item_free_func | used to pass as a free function to `g_async_queue_new_full()`. | [nullable]
max_threads | the maximal number of threads to execute concurrently in the new thread pool, `-1` means no limit |
exclusive | should this thread pool be exclusive? |
error | return location for error, or `NULL` |

#### Returns

the new GThreadPool.

[transfer full]

Since: 2.70

### g_thread_pool_push ()

    gboolean
    g_thread_pool_push (GThreadPool *pool,
                        gpointer data,
                        GError **error);

Inserts `data` into the list of tasks to be executed by `pool` .

When the number of currently running threads is lower than the maximal allowed number of threads, a new thread is started (or reused) with the properties given to `g_thread_pool_new()`. Otherwise, `data` stays in the queue until a thread in this pool finishes its previous task and processes `data` .

`error` can be `NULL` to ignore errors, or non-`NULL` to report errors. An error can only occur when a new thread couldn't be created. In that case `data` is simply appended to the queue of work to do.

Before version 2.32, this function did not return a success status.

#### Parameters

pool | a GThreadPool |
data | a new task for `pool` |
error | return location for error, or `NULL` |

#### Returns

`TRUE` on success, `FALSE` if an error occurred

### g_thread_pool_set_max_threads ()

    gboolean
    g_thread_pool_set_max_threads (GThreadPool *pool,
                                   gint max_threads,
                                   GError **error);

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.

Setting `max_threads` to 0 means stopping all work for `pool` . It is effectively frozen until `max_threads` is set to a non-zero value again.

A thread is never terminated while calling `func` , as supplied by `g_thread_pool_new()`. Instead the maximal number of threads only has effect for the allocation of new threads in `g_thread_pool_push()`. A new thread is allocated, whenever the number of currently running threads in `pool` is smaller than the maximal number.

`error` can be `NULL` to ignore errors, or non-`NULL` to report errors. An error can only occur when a new thread couldn't be created.

Before version 2.32, this function did not return a success status.

#### Parameters

pool | a GThreadPool |
max_threads | a new maximal number of threads for `pool` , or -1 for unlimited |
error | return location for error, or `NULL` |

#### Returns

`TRUE` on success, `FALSE` if an error occurred

### g_thread_pool_get_max_threads ()

    gint
    g_thread_pool_get_max_threads (GThreadPool *pool);

Returns the maximal number of threads for `pool` .

#### Parameters

pool | a GThreadPool |

#### Returns

the maximal number of threads

### g_thread_pool_get_num_threads ()

    guint
    g_thread_pool_get_num_threads (GThreadPool *pool);

Returns the number of threads currently running in `pool` .

#### Parameters

pool | a GThreadPool |

#### Returns

the number of threads currently running

### g_thread_pool_unprocessed ()

    guint
    g_thread_pool_unprocessed (GThreadPool *pool);

Returns the number of tasks still unprocessed in `pool` .

#### Parameters

pool | a GThreadPool |

#### Returns

the number of unprocessed tasks

### g_thread_pool_free ()

    void
    g_thread_pool_free (GThreadPool *pool,
                        gboolean immediate,
                        gboolean wait_);

Frees all resources allocated for `pool` .

If `immediate` is `TRUE`, no new task is processed for `pool` . Otherwise `pool` is not freed before the last task is processed. Note however, that no thread of this pool is interrupted while processing a task. Instead at least all still running threads can finish their tasks before the `pool` is freed.

If `wait_` is `TRUE`, this function does not return before all tasks to be processed (dependent on `immediate` , whether all or only the currently running) are ready. Otherwise this function returns immediately.

After calling this function `pool` must not be used anymore.

#### Parameters

pool | a GThreadPool |
immediate | should `pool` shut down immediately? |
wait_ | should the function wait for all tasks to be finished? |

### 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 `max_threads` . If `max_threads` is -1, no limit is imposed on the number of unused threads.

The default value is 2.

#### Parameters

max_threads | maximal number of unused threads |

### g_thread_pool_get_max_unused_threads ()

    gint
    g_thread_pool_get_max_unused_threads (void);

Returns the maximal allowed number of unused threads.

#### Returns

the maximal 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.

#### 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_timeout_add()`.

### 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 `func` , and not just in the order in which they were added to the pool.

Note, if the maximum number of threads is more than 1, the order that threads are executed cannot be guaranteed 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.

#### Parameters

pool | a GThreadPool |
func | the GCompareDataFunc used to sort the list of tasks. This function is passed two tasks. It should return 0 if the order in which they are handled does not matter, a negative value if the first task should be processed before the second or a positive value if the second task should be processed first. |
user_data | user data passed to `func` |

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 `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.

By setting `interval` to 0, idle threads will not be stopped.

The default value is 15000 (15 seconds).

#### Parameters

interval | the maximum `interval` (in milliseconds) a thread can be idle |

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 `interval` that a thread will wait in the thread pool for new tasks before being stopped.

If this function returns 0, threads waiting in the thread pool for new work are not stopped.

#### Returns

the maximum `interval` (milliseconds) to wait for new tasks in the thread pool before stopping the thread

Since: 2.10

### g_thread_pool_move_to_front ()

    gboolean
    g_thread_pool_move_to_front (GThreadPool *pool,
                                 gpointer data);

Moves the item to the front of the queue of unprocessed items, so that it will be processed next.

#### Parameters

pool | a GThreadPool |
data | an unprocessed item in the pool |

#### Returns

`TRUE` if the item was found and moved

Since: 2.46

## Types and Values

### struct GThreadPool

    struct GThreadPool {
      GFunc func;
      gpointer user_data;
      gboolean exclusive;
    };

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.

#### Members

GFunc `func`; | the function to execute in the threads of this pool |
gpointer `user_data`; | the user data for the threads of this pool |
gboolean `exclusive`; | are all threads exclusive to this pool |

## See Also

GThread