NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
thread_pool类 参考

线程池类 更多...

#include <thread_pool.hpp>

struct  periodic_task_state
 周期性任务状态 更多...
struct  pool_statistics
 线程池统计信息 更多...

Public 类型

enum class  pool_mode : uint8_t { fixed , cached }
 线程池运行模式 更多...
using steal_strategy = local_queue::steal_strategy
 窃取策略类型别名
using id_type = uint32_t
 线程ID类型别名
using periodic_token = shared_ptr<periodic_task_state>
 周期性任务令牌
using priority_type = task_info::priority_type
 优先级类型别名

Public 成员函数

 thread_pool ()
 默认构造函数
 ~thread_pool ()
 析构函数
bool set_mode (pool_mode mode) noexcept
 设置线程池模式
bool set_steal_mode (steal_strategy strategy, uint32_t steal_batch=4) noexcept
 设置窃取策略
bool set_task_threshhold (size_t threshhold) noexcept
 设置任务队列阈值
bool set_thread_threshhold (size_t threshhold) noexcept
 设置线程数阈值
bool running () const noexcept
 检查线程池是否正在运行
pool_mode mode () const noexcept
 获取线程池模式
pool_statistics statistics () const
 获取线程池统计信息
bool start (size_t init_thread_size=3)
 启动线程池
pool_statistics stop ()
 停止线程池
template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > submit_task (priority_type priority, Func &&func, Args &&... args)
 提交任务
template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > submit_task (Func &&func, Args &&... args)
 提交任务(使用默认优先级0)
template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > submit_after (int64_t delay_ms, priority_type priority, Func &&func, Args &&... args)
 提交延迟任务
template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > submit_after (int64_t delay_ms, Func &&func, Args &&... args)
 提交延迟任务(使用默认优先级0)
template<typename Func, typename... Args>
periodic_token submit_every (int64_t interval_ms, priority_type priority, Func &&func, Args &&... args)
 提交周期性任务
template<typename Func, typename... Args>
periodic_token submit_every (int64_t interval_ms, Func &&func, Args &&... args)
 提交周期性任务(使用默认优先级0)

静态 Public 成员函数

static void cancel_periodic_task (const periodic_token &token)
 取消周期性任务
template<typename... Types>
static tuple< future_result_t< Types >... > wait (future< Types > &&... futures)
 等待多个future完成

静态 Public 属性

static constexpr size_t task_max_threshhold = numeric_traits<int32_t>::max()
 最大任务队列阈值
static constexpr size_t max_idle_seconds = 60
 最大空闲秒数

详细描述

线程池类

高性能线程池实现,支持:

  • 固定模式和缓存模式
  • 任务优先级调度
  • 工作窃取算法
  • 延迟任务和周期性任务
  • 任务组跟踪

在文件 thread_pool.hpp308 行定义.

成员枚举类型说明

◆ pool_mode

enum class thread_pool::pool_mode : uint8_t
strong

线程池运行模式

枚举值
fixed 

固定线程数模式

cached 

缓存模式,可动态增减线程

在文件 thread_pool.hpp314 行定义.

成员函数说明

◆ cancel_periodic_task()

void thread_pool::cancel_periodic_task ( const periodic_token & token)
inlinestatic

取消周期性任务

参数
token周期性任务令牌

在文件 thread_pool.hpp587 行定义.

◆ mode()

pool_mode thread_pool::mode ( ) const
inlinenodiscardnoexcept

获取线程池模式

返回
当前模式

在文件 thread_pool.hpp479 行定义.

被这些函数引用 set_mode().

◆ running()

bool thread_pool::running ( ) const
inlinenodiscardnoexcept

检查线程池是否正在运行

返回
正在运行返回true

在文件 thread_pool.hpp473 行定义.

引用了 running().

被这些函数引用 running().

◆ set_mode()

bool thread_pool::set_mode ( pool_mode mode)
noexcept

设置线程池模式

参数
mode新模式
返回
设置成功返回true(线程池未运行时)

引用了 mode().

◆ set_steal_mode()

bool thread_pool::set_steal_mode ( steal_strategy strategy,
uint32_t steal_batch = 4 )
noexcept

设置窃取策略

参数
strategy窃取策略
steal_batch批次大小
返回
设置成功返回true(线程池未运行时)

◆ set_task_threshhold()

bool thread_pool::set_task_threshhold ( size_t threshhold)
noexcept

设置任务队列阈值

参数
threshhold新阈值
返回
设置成功返回true(线程池未运行时)

引用了 set_task_threshhold().

被这些函数引用 set_task_threshhold().

◆ set_thread_threshhold()

bool thread_pool::set_thread_threshhold ( size_t threshhold)
noexcept

设置线程数阈值

参数
threshhold新阈值
返回
设置成功返回true(线程池未运行时且处于缓存模式)

引用了 set_thread_threshhold().

被这些函数引用 set_thread_threshhold().

◆ start()

bool thread_pool::start ( size_t init_thread_size = 3)

启动线程池

参数
init_thread_size初始线程数,默认为3
返回
启动成功返回true

◆ statistics()

pool_statistics thread_pool::statistics ( ) const
nodiscard

获取线程池统计信息

返回
统计信息

◆ stop()

pool_statistics thread_pool::stop ( )

停止线程池

返回
停止前的统计信息

◆ submit_after() [1/2]

template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > thread_pool::submit_after ( int64_t delay_ms,
Func && func,
Args &&... args )
inline

提交延迟任务(使用默认优先级0)

模板参数
Func可调用对象类型
Args参数类型
参数
delay_ms延迟时间(毫秒)
func可调用对象
args参数
返回
包含future和任务信息的结果

在文件 thread_pool.hpp550 行定义.

引用了 forward() , 以及 submit_after().

◆ submit_after() [2/2]

template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > thread_pool::submit_after ( int64_t delay_ms,
priority_type priority,
Func && func,
Args &&... args )

提交延迟任务

模板参数
Func可调用对象类型
Args参数类型
参数
delay_ms延迟时间(毫秒)
priority任务优先级
func可调用对象
args参数
返回
包含future和任务信息的结果

引用了 priority().

被这些函数引用 submit_after().

◆ submit_every() [1/2]

template<typename Func, typename... Args>
periodic_token thread_pool::submit_every ( int64_t interval_ms,
Func && func,
Args &&... args )
inline

提交周期性任务(使用默认优先级0)

模板参数
Func可调用对象类型
Args参数类型
参数
interval_ms执行间隔(毫秒)
func可调用对象
args参数
返回
周期性任务令牌,用于取消任务

在文件 thread_pool.hpp578 行定义.

引用了 forward() , 以及 submit_every().

◆ submit_every() [2/2]

template<typename Func, typename... Args>
periodic_token thread_pool::submit_every ( int64_t interval_ms,
priority_type priority,
Func && func,
Args &&... args )

提交周期性任务

模板参数
Func可调用对象类型
Args参数类型
参数
interval_ms执行间隔(毫秒)
priority任务优先级
func可调用对象
args参数
返回
周期性任务令牌,用于取消任务

引用了 priority().

被这些函数引用 submit_every().

◆ submit_task() [1/2]

template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > thread_pool::submit_task ( Func && func,
Args &&... args )
inline

提交任务(使用默认优先级0)

模板参数
Func可调用对象类型
Args参数类型
参数
func可调用对象
args参数
返回
包含future和任务信息的结果

在文件 thread_pool.hpp521 行定义.

引用了 forward() , 以及 submit_task().

◆ submit_task() [2/2]

template<typename Func, typename... Args>
submit_result< invoke_result_t< Func, Args... > > thread_pool::submit_task ( priority_type priority,
Func && func,
Args &&... args )

提交任务

模板参数
Func可调用对象类型
Args参数类型
参数
priority任务优先级
func可调用对象
args参数
返回
包含future和任务信息的结果

引用了 priority().

被这些函数引用 submit_task().

◆ wait()

template<typename... Types>
tuple< future_result_t< Types >... > thread_pool::wait ( future< Types > &&... futures)
inlinestatic

等待多个future完成

模板参数
Typesfuture结果类型
参数
futures要等待的future
返回
包含所有结果的元组

在文件 thread_pool.hpp600 行定义.

引用了 get() , 以及 make_tuple().


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