Fixed-size thread pool for parallel task execution.
More...
#include <thread_pool.h>
|
| | ThreadPool (size_t thread_count=4U) |
| | Constructs a ThreadPool with thread_count worker threads and kNormalType queue.
|
| | ThreadPool (size_t thread_count, Type type) |
| | Constructs a ThreadPool with the specified thread count and queue type.
|
| virtual | ~ThreadPool () |
| | Destructor. Calls shutdown() and joins all worker threads.
|
| void | set_name (const std::string &name) |
| | Sets a human-readable name for the pool (and its worker threads).
|
| const std::string & | get_name () const |
| | Returns the name set via set_name().
|
| Type | get_type () const |
| | Returns the queue type this pool was constructed with.
|
| Strategy | get_strategy () const |
| | Returns the current idle strategy.
|
| void | set_strategy (Strategy strategy) |
| | Changes the idle strategy.
|
| bool | shutdown () |
| | Signals all workers to finish current tasks and exit.
|
| bool | post_task (Callback &&callback) |
| | Posts a task to the queue for execution by a worker thread.
|
| size_t | get_task_count () const |
| | Returns the number of tasks currently in the queue.
|
| bool | is_in_work_thread () const |
| | Returns true if the calling thread is a worker thread of this pool.
|
| virtual size_t | get_max_task_count () const |
| | Returns the maximum queue depth.
|
| template<class FunctionT, class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>> |
| std::future< ResultT > | invoke_task (FunctionT &&function, ArgsT &&... args) |
| | Dispatches a callable to a worker thread and returns a std::future for the result.
|
Fixed-size thread pool for parallel task execution.
Worker threads are created on construction and destroyed on shutdown() or destruction.
◆ Callback
Callback type for tasks submitted to the pool.
◆ Strategy
Idle strategy controlling CPU and wake-up latency trade-offs.
| Enumerator |
|---|
| kOptimizationStrategy | Balance latency and CPU via yield.
|
| kPopStrategy | Busy-poll (lowest latency, highest CPU).
|
| kBlockStrategy | Block on condition variable (lowest CPU).
|
◆ Type
Queue implementation type.
| Enumerator |
|---|
| kNormalType | Mutex-protected FIFO queue (default).
|
| kLockfreeType | Lock-free MPMC queue.
|
◆ ThreadPool() [1/2]
| vlink::ThreadPool::ThreadPool |
( |
size_t | thread_count = 4U | ) |
|
|
explicit |
Constructs a ThreadPool with thread_count worker threads and kNormalType queue.
- Parameters
-
| thread_count | Number of worker threads. Default: 4. |
◆ ThreadPool() [2/2]
| vlink::ThreadPool::ThreadPool |
( |
size_t | thread_count, |
|
|
Type | type ) |
|
explicit |
Constructs a ThreadPool with the specified thread count and queue type.
- Parameters
-
| thread_count | Number of worker threads. |
| type | Queue implementation type. |
◆ ~ThreadPool()
| virtual vlink::ThreadPool::~ThreadPool |
( |
| ) |
|
|
virtual |
Destructor. Calls shutdown() and joins all worker threads.
◆ get_max_task_count()
| virtual size_t vlink::ThreadPool::get_max_task_count |
( |
| ) |
const |
|
nodiscardvirtual |
Returns the maximum queue depth.
- Returns
- Maximum number of tasks that can be queued simultaneously.
◆ get_name()
| const std::string & vlink::ThreadPool::get_name |
( |
| ) |
const |
|
nodiscard |
Returns the name set via set_name().
- Returns
- Reference to the name string.
◆ get_strategy()
| Strategy vlink::ThreadPool::get_strategy |
( |
| ) |
const |
|
nodiscard |
Returns the current idle strategy.
- Returns
- Current strategy.
◆ get_task_count()
| size_t vlink::ThreadPool::get_task_count |
( |
| ) |
const |
|
nodiscard |
Returns the number of tasks currently in the queue.
- Returns
- Pending task count.
◆ get_type()
| Type vlink::ThreadPool::get_type |
( |
| ) |
const |
|
nodiscard |
Returns the queue type this pool was constructed with.
- Returns
- Queue type.
◆ invoke_task()
template<class FunctionT, class... ArgsT, typename ResultT>
| std::future< ResultT > vlink::ThreadPool::invoke_task |
( |
FunctionT && | function, |
|
|
ArgsT &&... | args ) |
|
inlinenodiscard |
Dispatches a callable to a worker thread and returns a std::future for the result.
Details.
Thread-safe. The future becomes ready after the callable is executed by a worker.
- Warning
- Do not block on the future from a pool worker thread if all workers are busy; this will deadlock.
- Template Parameters
-
| FunctionT | Callable type. |
| ArgsT | Argument types. |
| ResultT | Return type (deduced). |
- Parameters
-
| function | Callable to dispatch. |
| args | Arguments forwarded to the callable. |
- Returns
std::future<ResultT> that becomes ready when the task completes.
◆ is_in_work_thread()
| bool vlink::ThreadPool::is_in_work_thread |
( |
| ) |
const |
|
nodiscard |
Returns true if the calling thread is a worker thread of this pool.
- Returns
true if called from a pool worker.
◆ post_task()
| bool vlink::ThreadPool::post_task |
( |
Callback && | callback | ) |
|
Posts a task to the queue for execution by a worker thread.
Thread-safe. Returns false if the pool is shutting down or the queue is full (get_max_task_count()).
- Parameters
-
- Returns
true if enqueued successfully.
◆ set_name()
| void vlink::ThreadPool::set_name |
( |
const std::string & | name | ) |
|
Sets a human-readable name for the pool (and its worker threads).
- Parameters
-
◆ set_strategy()
| void vlink::ThreadPool::set_strategy |
( |
Strategy | strategy | ) |
|
Changes the idle strategy.
- Parameters
-
◆ shutdown()
| bool vlink::ThreadPool::shutdown |
( |
| ) |
|
Signals all workers to finish current tasks and exit.
Waits for all worker threads to join before returning. After shutdown(), the pool can no longer accept tasks.
- Returns
true on success.
The documentation for this class was generated from the following file: