11#include <condition_variable>
13#include "../logs/Xulog.h"
22 using ptr = std::unique_ptr<threadpool>;
23 using Functor = std::function<void(
void)>;
29 for (
int i = 0; i < thr_count; i++)
54 template <
typename F,
typename... Args>
55 auto push(F &&func, Args &&...args) -> std::future<
decltype(func(args...))>
58 using return_type =
decltype(func(args...));
59 auto tmp_func = std::bind(std::forward<F>(func), std::forward<Args>(args)...);
60 auto task = std::make_shared<std::packaged_task<return_type()>>(tmp_func);
61 std::future<return_type> fu = task->get_future();
64 std::unique_lock<std::mutex> lock(
_mutex);
81 std::vector<Functor> tmp_taskpool;
84 std::unique_lock<std::mutex> lock(
_mutex);
86 _cv.wait(lock, [
this]()
92 for (
auto &task : tmp_taskpool)
103 std::condition_variable
_cv;
线程池类
Definition threadpool.hpp:20
std::vector< std::thread > _threads
管理线程
Definition threadpool.hpp:104
threadpool(int thr_count=1)
构造函数
Definition threadpool.hpp:27
std::vector< Functor > _taskpool
任务池
Definition threadpool.hpp:101
std::unique_ptr< threadpool > ptr
线程池操作句柄
Definition threadpool.hpp:22
void entry()
线程入口函数 从任务池中取出任务执行
Definition threadpool.hpp:75
std::function< void(void)> Functor
线程池回调函数
Definition threadpool.hpp:23
std::condition_variable _cv
条件变量
Definition threadpool.hpp:103
std::atomic< bool > _stop
原子类型的停止标志
Definition threadpool.hpp:100
void stop()
停止所有线程
Definition threadpool.hpp:38
auto push(F &&func, Args &&...args) -> std::future< decltype(func(args...))>
传入任务函数到任务池
Definition threadpool.hpp:55
~threadpool()
析构函数
Definition threadpool.hpp:33
std::mutex _mutex
互斥锁
Definition threadpool.hpp:102