Qore Programming Language Reference Manual: Qore::Thread::ThreadPool Class Reference (original) (raw)

This class defines a thread pool that grows and shrinks dynamically within user-defined limits according to the task load placed on it. More...

This class defines a thread pool that grows and shrinks dynamically within user-defined limits according to the task load placed on it.

The ThreadPool can also pre-allocate idle threads for quickly allocating threads to tasks submitted through ThreadPool::submit() for cases when very low latency is required (for example, for allocating already waiting threads to incoming Socket connections).

A worker thread is started while the ThreadPool is running that waits for tasks in an internal task queue and allocates the tasks to worker threads. If an idle thread is available, the task is submitted to that thread immediately, otherwise, if the ThreadPool is not already at maximum capacity (the max argument to ThreadPool::constructor()), a new thread is started and the task is allocated to the new thread. Otherwise, the task will block until a thread becomes free, at which time the task is allocated to the newly-freed worker thread.

When a worker thread has no more tasks to execute, it will either be returned to the pool to wait in an idle state if possible, or it will terminate. Threads are returned to the idle pool if there are fewer than maxidle threads in the idle pool already or if there are more tasks in the queue than idle threads.

ThreadPools downscale over time when demand is lower according to the release_ms argument to ThreadPool::constructor(); when there more than minidle threads in the idle pool, then for each time period defined by release_ms, a single thread in the idle pool is released until the number of threads in the idle pool reaches minidle.

Therefore the minidle argument defines the "ground state" of the ThreadPool (how many idle threads are waiting for tasks), and the release_ms argument defines the period in which the ThreadPool returns to its ground state after demand for threads results in a condition where there are temporarily more than minidle threads in the idle pool.

If the ThreadPool is stopped when tasks are still in the queue, then any cancellation closure or call reference for the task is executed; see ThreadPool::submit() for more information.

Restrictions:

Qore::PO_NO_THREAD_CLASSES

Example:

Since

Qore 0.8.8