原文地址:https://zhuanlan.zhihu.com/p/61464921
我只是为了做笔记,格式太乱,建议直接看原文
网上摘来修改了一下:
threadpool.h:
#ifndef
THREADPOOL_H
#define
THREADPOOL_H
#include
<thread>
#include
<condition_variable>
#include
<mutex>
#include
<vector>
#include
<queue>
#include
<future>
class
ThreadPool
{
public:
explicit
ThreadPool(size_t
threadNum);
~ThreadPool();
template<typename
F,
typename...
Args>
auto
submit(F&&
f,
Args&&...
args)
->
std::future<decltype(f(args...))>;
private:
bool
stop_;
std::vector<std::thread>
workers_;
std::queue<std::function<void()>>
tasks_;
std::mutex
mtx_;
std::condition_variable
cv_;
};
template<typename
F,
typename...
Args>
auto
ThreadPool::submit(F&&
f,
Args&&...
args)
->
std::future<decl