VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
vlink::ThreadPool类 参考

Fixed-size thread pool for parallel task execution. 更多...

#include <thread_pool.h>

vlink::ThreadPool 的协作图:

Public 类型

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

Public 成员函数

 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

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

Callback type for tasks submitted to the pool.

成员枚举类型说明

◆ Strategy

Idle strategy controlling CPU and wake-up latency trade-offs.

枚举值
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.

枚举值
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.

参数
thread_countNumber 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.

参数
thread_countNumber of worker threads.
typeQueue 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.

返回
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().

返回
Reference to the name string.

◆ get_strategy()

Strategy vlink::ThreadPool::get_strategy ( ) const
nodiscard

Returns the current idle strategy.

返回
Current strategy.

◆ get_task_count()

size_t vlink::ThreadPool::get_task_count ( ) const
nodiscard

Returns the number of tasks currently in the queue.

返回
Pending task count.

◆ get_type()

Type vlink::ThreadPool::get_type ( ) const
nodiscard

Returns the queue type this pool was constructed with.

返回
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.

警告
Do not block on the future from a pool worker thread if all workers are busy; this will deadlock.
模板参数
FunctionTCallable type.
ArgsTArgument types.
ResultTReturn type (deduced).
参数
functionCallable to dispatch.
argsArguments forwarded to the callable.
返回
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.

返回
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()).

参数
callbackTask to execute.
返回
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).

参数
nameName string.

◆ set_strategy()

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

Changes the idle strategy.

参数
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.

返回
true on success.

该类的文档由以下文件生成: