GThreadPool

The glib.thread_pool.ThreadPool 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 glib.thread_pool.ThreadPool.new_. It is destroyed by glib.thread_pool.ThreadPool.free.

If you want to execute a certain task within a thread pool, use glib.thread_pool.ThreadPool.push.

To get the current number of running threads you call glib.thread_pool.ThreadPool.getNumThreads. To get the number of still unprocessed tasks you call glib.thread_pool.ThreadPool.unprocessed. To control the maximum number of threads for a thread pool, you use glib.thread_pool.ThreadPool.getMaxThreads. and glib.thread_pool.ThreadPool.setMaxThreads.

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 glib.thread_pool.ThreadPool.getNumUnusedThreads. The maximum number can be controlled by glib.thread_pool.ThreadPool.getMaxUnusedThreads and glib.thread_pool.ThreadPool.setMaxUnusedThreads. All currently unused threads can be stopped by calling glib.thread_pool.ThreadPool.stopUnusedThreads.

Members

Variables

exclusive
bool exclusive;

are all threads exclusive to this pool

func
GFunc func;

the function to execute in the threads of this pool

userData
void* userData;

the user data for the threads of this pool