glib.thread_pool

Module for [ThreadPool] class

Types 1

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

Fields
GThreadPool _cInstance
Methods
void * _cPtr()
GFunc func() @propertyGet `func` field. Returns: the function to execute in the threads of this pool
void func(GFunc propval) @propertySet `func` field. Params: propval = the function to execute in the threads of this pool
bool exclusive() @propertyGet `exclusive` field. Returns: are all threads exclusive to this pool
void exclusive(bool propval) @propertySet `exclusive` field. Params: propval = are all threads exclusive to this pool
private void freePool(GThreadPool * pool)
int getMaxThreads()Returns the maximal number of threads for pool. Returns: the maximal number of threads
uint getNumThreads()Returns the number of threads currently running in pool. Returns: the number of threads currently running
bool moveToFront(void * data = null)Moves the item to the front of the queue of unprocessed items, so that it will be processed next.
bool push(void * data = null)Inserts data into the list of tasks to be executed by pool.
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 ...
uint unprocessed()Returns the number of tasks still unprocessed in pool. Returns: the number of unprocessed tasks
uint getMaxIdleTime()This function will return the maximum interval that a thread will wait in the thread pool for new tasks before being stopped.
int getMaxUnusedThreads()Returns the maximal allowed number of unused threads. Returns: the maximal number of unused threads
uint getNumUnusedThreads()Returns the number of currently unused threads. Returns: the number of currently unused threads
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.st...
void setMaxUnusedThreads(int maxThreads)Sets the maximal number of unused threads to maxthreads. If maxthreads is -1, no limit is imposed on the number of unused threads.
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].
Constructors
this(void * ptr, Flag!"Take" take)
Destructors