VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::ThreadPool Class Reference

Fixed-size thread pool for parallel task execution. More...

#include <thread_pool.h>

Collaboration diagram for vlink::ThreadPool:

Public Types

enum  Type : uint8_t { kNormalType = 0 , kLockfreeType = 1 }
 Queue implementation type. More...
enum  Strategy : uint8_t { kOptimizationStrategy = 0 , kPopStrategy = 1 , kBlockStrategy = 2 }
 Idle strategy controlling CPU and wake-up latency trade-offs. More...
using Callback = std::function<void()>
 Callback type for tasks submitted to the pool.

Public Member Functions

 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.

Detailed Description

Fixed-size thread pool for parallel task execution.

Worker threads are created on construction and destroyed on shutdown() or destruction.

Member Typedef Documentation

◆ Callback

using vlink::ThreadPool::Callback = std::function<void()>

Callback type for tasks submitted to the pool.

Member Enumeration Documentation

◆ 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

enum vlink::ThreadPool::Type : uint8_t

Queue implementation type.

Enumerator
kNormalType 

Mutex-protected FIFO queue (default).

kLockfreeType 

Lock-free MPMC queue.

Constructor & Destructor Documentation

◆ 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_countNumber of worker threads. Default: 4.
Here is the caller graph for this function:

◆ 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_countNumber of worker threads.
typeQueue implementation type.

◆ ~ThreadPool()

virtual vlink::ThreadPool::~ThreadPool ( )
virtual

Destructor. Calls shutdown() and joins all worker threads.

Member Function Documentation

◆ 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.
Here is the call graph for this function:

◆ 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
FunctionTCallable type.
ArgsTArgument types.
ResultTReturn type (deduced).
Parameters
functionCallable to dispatch.
argsArguments forwarded to the callable.
Returns
std::future<ResultT> that becomes ready when the task completes.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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
callbackTask to execute.
Returns
true if enqueued successfully.
Here is the caller graph for this function:

◆ set_name()

void vlink::ThreadPool::set_name ( const std::string & name)

Sets a human-readable name for the pool (and its worker threads).

Parameters
nameName string.

◆ set_strategy()

void vlink::ThreadPool::set_strategy ( Strategy strategy)

Changes the idle strategy.

Parameters
strategyNew strategy.

◆ 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: