ThreadPool

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.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

getMaxThreads
int getMaxThreads()

Returns the maximal number of threads for pool.

getNumThreads
uint getNumThreads()

Returns the number of threads currently running in pool.

moveToFront
bool moveToFront(void* data)

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

push
bool push(void* data)

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

setMaxThreads
bool setMaxThreads(int maxThreads)

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.

unprocessed
uint unprocessed()

Returns the number of tasks still unprocessed in pool.

Static functions

getMaxIdleTime
uint getMaxIdleTime()

This function will return the maximum interval that a thread will wait in the thread pool for new tasks before being stopped.

getMaxUnusedThreads
int getMaxUnusedThreads()

Returns the maximal allowed number of unused threads.

getNumUnusedThreads
uint getNumUnusedThreads()

Returns the number of currently unused threads.

setMaxIdleTime
void setMaxIdleTime(uint 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 glib.thread_pool.ThreadPool.stopUnusedThreads on a regular timeout, except this is done on a per thread basis.

setMaxUnusedThreads
void setMaxUnusedThreads(int maxThreads)

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.

stopUnusedThreads
void stopUnusedThreads()

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 glib.global.timeoutAdd.