线程池学习

1.source code

https://github.com/philipphenkel/threadpool/tree/a7b3e058e3416c1cb819b70948274367c80bb2e3

2.线程池需要考虑的因素

template <
    typename Task                                   = task_func,
    template <typename> class SchedulingPolicy      = fifo_scheduler,
    template <typename> class SizePolicy            = static_size,
    template <typename> class SizePolicyController  = resize_controller,
    template <typename> class ShutdownPolicy        = wait_for_all_tasks
  > 
    volatile size_t m_worker_count;	            //  当前线程池中worker数量
    volatile size_t m_target_worker_count;	    //  期望值   线程池中的worker数
    volatile size_t m_active_worker_count;      //  正在执行task的worker数量  

3.核心变量

    mutable recursive_mutex  m_monitor;
    mutable condition m_worker_idle_or_terminated_event;	// A worker is idle or was terminated.
    mutable condition m_task_or_terminate_workers_event;  // Task is available OR total worker count should be reduced.

4.核心方法(注释部分为核心代码)

// worker_thread<pool_type>::create_and_attach(lockedThis->shared_from_this());
bool resize(size_t const worker_count) volatile

// lockedThis->m_worker_idle_or_terminated_event.notify_all();	
void worker_destructed(shared_ptr<worker_type> worker) volatile

// self->m_terminate_all_workers = true;
// m_target_worker_count = 0;
// self->m_task_or_terminate_workers_event.notify_all();
void terminate_all_workers(bool const wait) volatile

// while(lockedThis->m_scheduler.empty())
// {	
//   // decrease number of workers if necessary
//   if(m_worker_count > m_target_worker_count)
//   {	
//     return false;	// terminate worker
//   }
//   else
//   {
//     m_active_worker_count--;
//     lockedThis->m_worker_idle_or_terminated_event.notify_all();	
//     lockedThis->m_task_or_terminate_workers_event.wait(lock);
//     m_active_worker_count++;
//   }
// }
bool execute_task() volatile

5.核心类

pool_core

thread_worker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值