MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
| Public 成员函数 | Private 类型 | Private 成员函数 | Private 属性 | 所有成员列表
common::ThreadPoolExecutor类 参考

struct  ThreadData
 

Public 成员函数

int init (const char *name, int core_size, int max_size, long keep_alive_time_ms)
 初始化线程池 更多...
 
int init (const char *name, int core_pool_size, int max_pool_size, long keep_alive_time_ms, unique_ptr< Queue< unique_ptr< Runnable > > > &&work_queue)
 初始化线程池 更多...
 
int execute (unique_ptr< Runnable > &&task)
 提交一个任务,不一定可以立即执行 更多...
 
int execute (const function< void()> &callable)
 提交一个任务,不一定可以立即执行 更多...
 
int shutdown ()
 关闭线程池
 
int await_termination ()
 等待线程池处理完所有任务并退出
 
int active_count () const
 当前活跃线程的个数,就是正在处理任务的线程个数
 
int core_pool_size () const
 核心线程个数
 
int pool_size () const
 线程池中线程个数
 
int largest_pool_size () const
 曾经达到过的最大线程个数
 
int64_t task_count () const
 处理过的任务个数
 
int64_t queue_size () const
 任务队列中的任务个数
 

Private 类型

enum class  State { NEW , RUNNING , TERMINATING , TERMINATED }
 线程池的状态 更多...
 

Private 成员函数

int create_thread (bool core_thread)
 创建一个线程 更多...
 
int create_thread_locked (bool core_thread)
 创建一个线程。调用此函数前已经加锁 更多...
 
int extend_thread ()
 检测是否需要扩展线程,如果需要就扩展
 
void thread_func ()
 线程函数。从队列中拉任务并执行 更多...
 

Private 属性

State state_ = State::NEW
 
int core_pool_size_ = 0
 线程池状态
 
int max_pool_size_ = 0
 核心线程个数
 
chrono::milliseconds keep_alive_time_ms_
 最大线程个数
 
unique_ptr< Queue< unique_ptr< Runnable > > > work_queue_
 非核心线程空闲多久后退出
 
mutex lock_
 任务队列
 
map< thread::id, ThreadDatathreads_
 保护线程池内部数据的锁
 
int largest_pool_size_ = 0
 线程列表
 
atomic< int64_t > task_count_ = 0
 历史上达到的最大的线程个数
 
atomic< int > active_count_ = 0
 处理过的任务个数
 
string pool_name_
 活跃线程个数
 

成员枚举类型说明

◆ State

enum class common::ThreadPoolExecutor::State
strongprivate

线程池的状态

枚举值
RUNNING 

新建状态

TERMINATING 

正在运行

TERMINATED 

正在停止

已经停止

成员函数说明

◆ create_thread()

int common::ThreadPoolExecutor::create_thread ( bool  core_thread)
private

创建一个线程

参数
core_thread是否是核心线程

◆ create_thread_locked()

int common::ThreadPoolExecutor::create_thread_locked ( bool  core_thread)
private

创建一个线程。调用此函数前已经加锁

参数
core_thread是否是核心线程

◆ execute() [1/2]

int common::ThreadPoolExecutor::execute ( const function< void()> &  callable)

提交一个任务,不一定可以立即执行

参数
callable任务
返回
int 成功放入队列返回0

◆ execute() [2/2]

int common::ThreadPoolExecutor::execute ( unique_ptr< Runnable > &&  task)

提交一个任务,不一定可以立即执行

参数
task任务
返回
int 成功放入队列返回0

◆ init() [1/2]

int common::ThreadPoolExecutor::init ( const char *  name,
int  core_pool_size,
int  max_pool_size,
long  keep_alive_time_ms,
unique_ptr< Queue< unique_ptr< Runnable > > > &&  work_queue 
)

初始化线程池

参数
name线程池名称
core_size核心线程个数。核心线程不会退出
max_size线程池最大线程个数
keep_alive_time_ms非核心线程空闲多久后退出
work_queue任务队列

◆ init() [2/2]

int common::ThreadPoolExecutor::init ( const char *  name,
int  core_size,
int  max_size,
long  keep_alive_time_ms 
)

初始化线程池

参数
name线程池名称
core_size核心线程个数。核心线程不会退出
max_size线程池最大线程个数
keep_alive_time_ms非核心线程空闲多久后退出

◆ thread_func()

void common::ThreadPoolExecutor::thread_func ( )
private

线程函数。从队列中拉任务并执行

这里使用最粗暴的方式检测线程是否可以退出了 但是实际上,如果当前的线程个数比任务数要多,或者差不多,而且任务执行都很快的时候, 并不需要保留这么多线程


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