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.
Thread<ToolKit>
#include <ospace/thread/thrdpool.h>
class os_thread_pool
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;
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.wait_for_completion()
with a false argument.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
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
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
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
max_idle_threads() count.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.true if the
pool is closed and is in the process of shutting down.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.max_idle_threads()
limit.keep_alive_time()
before they are discarded.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.