thread_pool


A thread pool is a collection of reusable threads that are used repeatedly to run small tasks. The pool size grows up to a maximum of M threads and has a maximum of N idle threads in the pool at a given time. These threads are used repeatedly to execute small tasks. Each task is an object that implements a runnable interface, i.e. the void* run() method is called when it is executed.

Tasks can be added to the thread pool for execution using the execute() method. If there are no idle threads in the pool, new threads are created to execute the tasks until the total number of executing threads reaches M. After the maximum pool size is reached and if a task cannot be executed immediately, the task is added to a task queue. When threads become idle, waiting tasks are removed from the queue and executed. In the event, if the queue becomes empty, only N idle threads are kept around and the extra idle threads are terminated so that there are no more than N idle threads at any given time.

A keep alive time can be specified for threads that become idle after the max idle thread limit of N is reached. These keep alive idle threads wait for the specified keep alive time before they exit. If any task is enqueued and if the thread waiting for keep alive time is chosen, it is rescheduled to do the work like other N idle threads.

Use of member template feature in the execute methods gives the flexibility of not restricting the thread pool instance to a specific task class. Therefore, the compiler must be able to support member templates and OS_ENABLE_MEMBER_TEMPLATE_FEATURES must be defined to use this class.

Library

Thread<ToolKit>

Declaration

#include <ospace/thread/thrdpool.h>

class os_thread_pool

Typedefs

typedef vector< os_thread_t > os_thread_vector;
typedef priority_queue< os_task, less< os_task > > os_priority_task_queue;
typedef queue< os_task, deque< task > > os_fifo_task_queue;

Interface

Constructor
os_thread_pool( size_t max_pool_size , size_t max_idle_threads , long keep_alive_time )
Constructs a thread pool object with the specified limits. The maximum number of threads the pool can create is set to max_pool_size (default 100 ). The maximum number of idle threads the pool can cache is set to max_idle_threads (default 10 ). Any threads that become idle after the max_idle_threads limit is reached are kept for keep_alive_time milliseconds before they are discarded. If keep_alive_time is 0 (default), then any extra threads that become idle after the max_idle_threads limit is reached are discarded immediately.
Destructor
~os_thread_pool()
Gracefully closes the thread pool by calling wait_for_completion() with a false argument.
execute
template< class Task >
void execute( Task* task , void priority )
Schedules the runnable task for execution. The void* run() method is invoked on the task when it is executed. If there are any idle threads in the pool or if the pool size is less than max_pool_size(), then the task will be executed immediately. If it cannot be run immediately, it is queued with other tasks for execution at a later time. Waiting tasks with a non-zero priority (default 0 ) are given preference over the tasks with no priority at all (zero priority). Tasks with a priority are executed in priority order and tasks with no priority are executed in a FIFO order after all the tasks with priority are executed. Higher priority tasks are executed before the lower priority tasks, followed by no priority tasks. If this method is called after the pool is closed by calling wait_for_completion(), then an exception is thrown.

Throws: os_thread_toolkit_error

execute
template< class Task >
void execute( Task* task , size_t stack_size ,  void priority )
Schedules the runnable task for execution. The void* run() method is invoked on the task when it is executed. If there are any idle threads in the pool or if the pool size is less than max_pool_size(), then the task will be executed immediately. If it cannot be run immediately, it is queued with other tasks for execution at a later time. statck_size is used to specify the stack size for a newly created thread which is added into the pool. Waiting tasks with a non-zero priority (default 0 ) are given preference over the tasks with no priority at all (zero priority). Tasks with a priority are executed in priority order and tasks with no priority are executed in a FIFO order after all the tasks with priority are executed. Higher priority tasks are executed before the lower priority tasks, followed by no priority tasks. If this method is called after the pool is closed by calling wait_for_completion(), then an exception is thrown. 

Throws: os_thread_toolkit_error

execute_now
template< class Task >
void execute_now( Task* task )
Executes the runnable task immediately. All thread limits are ignored. If there are no idle threads in the pool or if the pool size has reached the max limit of max_pool_size(), a new thread is still created ignoring all limits to execute the task immediately. Upon completing the task, the thread is added back to the pool or may be destroyed so that all thread limits are maintained. If this method is called after the pool is closed by calling wait_for_completion(), then an exception is thrown.

Throws: os_thread_toolkit_error

execute_now
template< class Task >
void execute_now( Task* task, size_t stack_size )
Executes the runnable task immediately. statck_size is used to specify the stack size for a newly created thread which is added into the pool. All thread limits are ignored. If there are no idle threads in the pool or if the pool size has reached the max limit of max_pool_size(), a new thread is still created ignoring all limits to execute the task immediately. Upon completing the task, the thread is added back to the pool or may be destroyed so that all thread limits are maintained. If this method is called after the pool is closed by calling wait_for_completion(), then an exception is thrown.

Throws: os_thread_toolkit_error

idle_threads
size_t idle_threads() const
Returns a snap shot of the number of idle threads in the pool blocked indefinitely waiting for work. This count never exceeds the max_idle_threads() count.
is_closed
bool is_closed() const
Returns true if the pool is closed for adding new tasks. The pool becomes closed when the wait_for_completion() method is invoked on the pool.
is_shutdown_in_progress
bool is_shutdown_in_progress() const
Returns true if the pool is closed and is in the process of shutting down.
keep_alive_idle_threads
size_t keep_alive_idle_threads() const
Returns a snap shot of the number of idle threads in the pool kept alive using keep_alive_time() time. Any threads that become idle after the max_idle_threads() limit is reached are kept alive for keep_alive_time() before they are released.
keep_alive_time
long keep_alive_time() const
Returns the keep alive time in milliseconds an idle thread will be kept alive after the number of idle threads reaches the max_idle_threads() limit.
keep_alive_time
void keep_alive_time( long keep_alive_msecs )
Sets the keep alive time to keep_alive_msecs milliseconds. After the number of idle thread reaches the maximum number of idle threads, any threads that become idle are kept alive for keep_alive_msecs milliseconds before are discarded. The keep alive threads could be reused if new tasks are added before the keep alive time expires.
max_idle_threads
size_t max_idle_threads() const
Returns the maximum number of idle threads in the pool that can be blocked indefinitely.
max_idle_threads
void max_idle_threads( size_t max_idle )
Sets the maximum number of idle threads. The maximum number of idle threads that are blocked indefinitely is set to max_idle . Any threads that become idle after the max_idle limit is reached are kept for keep_alive_time() before they are discarded.
max_pool_size
size_t max_pool_size() const
Returns the maximum number of threads the pool can create.
max_pool_size
void max_pool_size( size_t max_pool )
Sets the maximum number of threads the pool can create to max_pool . If max_pool is greater than the current maximim pool size and if there are any pending tasks in the queue, then appropriate number of threads are created to service the pending tasks. If max_pool is lesser than the current maximum pool size, then extra threads are discarded when they become idle.
pending_tasks
size_t pending_tasks() const
Returns a snap shot of the number of pending tasks in the task queue waiting for execution.
pool_size
size_t pool_size() const
Returns a snap short of the total number of thread currently in the pool. This includes the active and the idle threads.
shutdown
void shutdown()
Shutdowns the thread pool immediately. The pool is closed for adding new tasks. The pending tasks in the queue will not be executed. Thread termination signal is sent to all threads in the pool. The threads may or may not finish their currently executing tasks. The thread pool object is unusable after calling this method.
wait_for_completion
void wait_for_completion( bool ignore_pending_tasks )
Gracefully closes the thread pool. The pool is closed for adding new tasks. Any tasks currently executing will be run to completion. The pending tasks in the task queue will not be executed if the ignore_pending_tasks argument is true . The calling thread will block until all threads in the pool finish executing.

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.