考虑线程池工作线程处理完任务的第一个状态
- 处理之后请求队列为空,该线程回到等待状态
- 处理完当前任务后队列不为空,将与其他线程继续争夺锁,获取处理任务的资格
实现简易的线程池
c++简易的线程池包含线程数量,启动标志位,线程列表以及条件变量。
- 构造函数声明线程数量
- start为启动线程池,将n个线程绑定threadfunc自定义函数并执行,加入线程列表
- stop暂时停止线程,并由条件变量通知所有线程
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <stdexcept>
namespace yjx {
class ThreadPool{
public:
ThreadPool(size_t threads);
~ThreadPool();
public:
// typename F接受任意可调用的对象类型
// typename... args接受任务所需的任意数量、任意类型的参数
// F&& f, Args&&... args完美转发
template<typename F, typename... Args>
void enqueue(F&& f, Args&&... args);
};
private:
std::vector<std::thread> m_threads;
std::queue<std::function<void()>> m_tasks;
std::mutex m_mutex;
std::condition_variable m_cd;
bool stopping;
}
- 将任务 f 和参数 args... 打包为一个函数对象 f(args...)
- 将此单元放入线程安全的队列中
- 工作线程取任务执行
构造函数
创建了一个线程池
- 每一个线程循环访问任务队列,任务队列为空时就等待阻塞在上面等待唤醒
- 拿到任务后要处理队列,然后执行,然后继续等待任务队列
ThreadPool(size_t threads) : stopping(false){
for (size_t i = 0; i < threads; i++) {
// emplace_back,直接在容器内部构造对象,避免拷贝和引用
// push_back的话需要先构造一个thread对象
m_threads.emplace_back([this] {
while (true)
{
std::function<void()> task; // 任务
{
std::unique_lock<std::mutex> lock(this->m_mutex);
// wait 没有任务或者停止线程池就一直阻塞线程
this->m_cd.wait(lock, [this]{
return this->stopping || !this->m_tasks.empty();
});
if (this->stopping && this->m_tasks.empty())
{
return;
}
// 取任务
task = std::move(this->m_tasks.front());
// 任务被取走
this->m_tasks.pop();
}
task();// 执行任务
}
});
}
}
析构函数
- 首先将停止标志位置位(锁)
- 唤醒所有线程,清空任务队列
- join等待线程执行完毕回收资源
~ThreadPool(){
{
std::unique_lock<std::mutex> lock(m_mutex);
stopping = true;
}
m_cd.notify_all();
for (std::thread& worker : m_threads)
{
worker.join(); // 线程结束回收资源
}
}
设置任务队列
template<typename F, typename... Args>
void enqueue(F&& f, Args&&... args) {
std::function<void()> task = std::function<void()> task = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
{
std::unique_lock<std::mutex> lock(m_mutex);
m_tasks.emplace(std::move(task));
}
m_cd.notify_one();
测试
int main() {
// 创建线程池(4个工作线程)
ThreadPool pool(4);
// 提交任务
for (int i = 0; i < 8; ++i) {
pool.enqueue([i] {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Task " << i << " executed by thread "
<< std::this_thread::get_id() << std::endl;
});
}
return 0;
}