VLink  2.0.0
A high-performance communication middleware
vlink::ThreadPool Class Reference

Fixed worker pool that consumes a shared task queue. More...

#include <thread_pool.h>

Collaboration diagram for vlink::ThreadPool:

Public Types

enum  Type : uint8_t { kNormalType = 0 , kLockfreeType = 1 }
 Queue implementation backing the pool. More...
 
enum  Strategy : uint8_t { kOptimizationStrategy = 0 , kPopStrategy = 1 , kBlockStrategy = 2 }
 Submission-side strategy applied when the bounded queue is full. More...
 
using Callback = MoveFunction< void()>
 Callback signature used for submitted tasks. More...
 

Public Member Functions

 ThreadPool (size_t thread_count=4U)
 Constructs a pool with thread_count workers and the default kNormalType queue. More...
 
 ThreadPool (size_t thread_count, Type type)
 Constructs a pool with a custom queue implementation. More...
 
virtual ~ThreadPool ()
 Destructor. Calls shutdown() and joins worker threads. More...
 
void set_name (const std::string &name)
 Assigns a human-readable name to the pool and its workers (used by debuggers). More...
 
const std::string & get_name () const
 Returns the display name assigned via set_name(). More...
 
Type get_type () const
 Returns the queue implementation used by this pool. More...
 
Strategy get_strategy () const
 Returns the current submission-side back-pressure strategy. More...
 
void set_strategy (Strategy strategy)
 Updates the submission-side back-pressure strategy. More...
 
bool shutdown ()
 Marks the pool as quitting, drains in-flight tasks, and joins workers. More...
 
bool post_task (Callback &&callback)
 Submits a task to the queue for execution by a worker thread. More...
 
TaskHandle post_task_handle (Callback &&callback, const PostTaskOptions &options={})
 Submits a task that produces an observable TaskHandle. More...
 
size_t get_task_count () const
 Returns the current number of tasks waiting in the queue. More...
 
bool is_in_work_thread () const
 Reports whether the calling thread is one of this pool's workers. More...
 
virtual size_t get_max_task_count () const
 Returns the maximum queue capacity. More...
 
template<class FunctionT , class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
std::future< ResultT > invoke_task (FunctionT &&function, ArgsT &&... args)
 Submits a callable to a worker thread and returns a std::future for the result. More...
 

Detailed Description

Fixed worker pool that consumes a shared task queue.

Worker threads are created during construction and joined during shutdown() or destruction. The pool object itself is non-copyable.

Member Typedef Documentation

◆ Callback

Callback signature used for submitted tasks.

Move-only (MoveFunction<void()>); see MessageLoop::Callback for rationale.

Member Enumeration Documentation

◆ Strategy

Submission-side strategy applied when the bounded queue is full.

Worker wake-up is independent of this enum. It controls only how post_task and invoke_task react when capacity is reached.

Enumerator
kOptimizationStrategy 

Retry up to 10 times with 1 ms sleep; then drop one eligible task and push.

kPopStrategy 

Immediately drop one eligible task and push the new one.

kBlockStrategy 

Retry indefinitely with 1 ms sleep until capacity frees up.

◆ Type

enum vlink::ThreadPool::Type : uint8_t

Queue implementation backing the pool.

Enumerator
kNormalType 

Default mutex-protected FIFO queue.

kLockfreeType 

Lock-free MPMC queue.

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

vlink::ThreadPool::ThreadPool ( size_t  thread_count = 4U)
explicit

Constructs a pool with thread_count workers and the default kNormalType queue.

Parameters
thread_countWorker thread count. Default: 4. Zero leaves the pool in the shutdown state.

◆ ThreadPool() [2/2]

vlink::ThreadPool::ThreadPool ( size_t  thread_count,
Type  type 
)
explicit

Constructs a pool with a custom queue implementation.

Parameters
thread_countWorker thread count. Zero leaves the pool in the shutdown state.
typeQueue implementation type.

◆ ~ThreadPool()

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

Destructor. Calls shutdown() and joins worker threads.

Member Function Documentation

◆ get_max_task_count()

virtual size_t vlink::ThreadPool::get_max_task_count ( ) const
virtual

Returns the maximum queue capacity.

Returns
Maximum number of tasks that may be queued at the same time.

◆ get_name()

const std::string& vlink::ThreadPool::get_name ( ) const

Returns the display name assigned via set_name().

Returns
Reference to the stored name string.

◆ get_strategy()

Strategy vlink::ThreadPool::get_strategy ( ) const

Returns the current submission-side back-pressure strategy.

Returns
Current strategy.

◆ get_task_count()

size_t vlink::ThreadPool::get_task_count ( ) const

Returns the current number of tasks waiting in the queue.

Returns
Pending task count.

◆ get_type()

Type vlink::ThreadPool::get_type ( ) const

Returns the queue implementation used by this pool.

Returns
Queue type.

◆ invoke_task()

template<class FunctionT , class... ArgsT, typename ResultT >
std::future< ResultT > vlink::ThreadPool::invoke_task ( FunctionT &&  function,
ArgsT &&...  args 
)
inline

Submits a callable to a worker thread and returns a std::future for the result.

Details.

Thread-safe. The future is satisfied once the callable returns. When posting fails the future becomes ready with a broken_promise / future_error result.

Warning
Do not block on the returned future from a pool worker thread while all workers are busy; doing so deadlocks the pool.
Template Parameters
FunctionTCallable type.
ArgsTArgument types forwarded to the callable.
ResultTDeduced result type.
Parameters
functionCallable to dispatch.
argsArguments to forward.
Returns
Future resolved with the callable's result.
Here is the call graph for this function:

◆ is_in_work_thread()

bool vlink::ThreadPool::is_in_work_thread ( ) const

Reports whether the calling thread is one of this pool's workers.

Returns
true when called from a worker.

◆ post_task()

bool vlink::ThreadPool::post_task ( Callback &&  callback)

Submits a task to the queue for execution by a worker thread.

Thread-safe. Returns false when the pool is already shut down or when overflow handling cannot make room for the new task. Overflow behaviour depends on the configured Strategy:

  • kOptimizationStrategy: retry up to 10 times with a 1 ms sleep, then drop one eligible task and push the new one.
  • kPopStrategy: drop one eligible task immediately and push the new one.
  • kBlockStrategy: retry indefinitely with 1 ms sleep until space is available.
Note
Drop-policy semantics:
  • kNormalType respects TaskDropPolicy::kProtected; protected tasks are never selected as eviction victims, and if every queued task is protected the post fails and returns false.
  • kLockfreeType does not track per-task drop policy; overflow drop simply removes one queued task regardless of how it was submitted.
Parameters
callbackTask to execute.
Returns
true when the task was eventually enqueued.
Here is the caller graph for this function:

◆ post_task_handle()

TaskHandle vlink::ThreadPool::post_task_handle ( Callback &&  callback,
const PostTaskOptions options = {} 
)

Submits a task that produces an observable TaskHandle.

Tracked counterpart of post_task(). The returned handle allows callers to wait for completion, request cooperative cancellation, and observe whether the task was rejected or dropped before execution.

Parameters
callbackTask to execute.
optionsOptional overflow, drop, and cancellation policy.
Returns
Handle observing the posted task; the handle remains valid even when the post is rejected so callers can inspect state().

◆ set_name()

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

Assigns a human-readable name to the pool and its workers (used by debuggers).

Parameters
nameDisplay name.

◆ set_strategy()

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

Updates the submission-side back-pressure strategy.

Parameters
strategyNew strategy.

◆ shutdown()

bool vlink::ThreadPool::shutdown ( )

Marks the pool as quitting, drains in-flight tasks, and joins workers.

After shutdown() returns, further submissions are rejected. Workers complete the task they are currently running plus any already-queued tasks before exiting. When called from a worker thread the calling worker's handle is detached instead of joined because a thread cannot join itself; the Impl block is kept alive via std::shared_ptr so the detached worker continues to see a valid pool state until it returns.

Returns
true on the first successful shutdown; false on subsequent calls.

The documentation for this class was generated from the following file: