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
[func
gLib.ThreadPool.new]. It is destroyed by
threadPoolFree.
If you want to execute a certain task within a thread pool, use
threadPoolPush.
To get the current number of running threads you call
threadPoolGetNumThreads. To get the number of still unprocessed
tasks you call
threadPoolUnprocessed. To control the maximum
number of threads for a thread pool, you use
threadPoolGetMaxThreads. and
threadPoolSetMaxThreads.
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
[func
gLib.ThreadPool.get_num_unused_threads]. The
maximum number can be controlled by
[func
gLib.ThreadPool.get_max_unused_threads] and
[func
gLib.ThreadPool.set_max_unused_threads]. All
currently unused threads can be stopped by calling
[func
gLib.ThreadPool.stop_unused_threads].