C++实现简单的线程池


// thread_pool.h
#pragma once
#include <vector>
#include <deque>
#include <thread>
#include <functional>
#include <condition_variable>

class ThreadPool {
    using Task = std::function<void()>;
    using TaskList = std::deque<Task>;
    using WorkThreadQueue = std::vector<std::thread*>;

public:
    ThreadPool();
    ~ThreadPool();

public:
    bool Start(uint16_t ThreadNum = 1);
    void Stop();
    void AddTask(const Task&);

private:
    void ThreadLoop();
    Task AcceptTask();

private:
    uint16_t thread_num_;
    bool is_started_;
    WorkThreadQueue work_thread_list_;
    TaskList task_list_;
    std::mutex thread_pool_mutex_;
    std::condition_variable condition_variable_;
};
// thread_pool.cpp
#include "thread_pool.h"

ThreadPool::ThreadPool() : thread_num_(1), is_started_(false) {}

ThreadPool::~ThreadPool() {
    if (true == is_started_) {
        Stop();
    }
}

bool ThreadPool::Start(uint16_t ThreadNum) {
    thread_num_ = ThreadNum;
    if (false == work_thread_list_.empty()) {
        return false;
    }
    is_started_ = true;
    //预先创建线程
    work_thread_list_.reserve(thread_num_);
    for (uint16_t i = 0; i < thread_num_; ++i) {
        work_thread_list_.push_back(new std::thread(std::bind(&ThreadPool::ThreadLoop, this)));
    }
    return true;
}

void ThreadPool::Stop() {
    std::lock_guard<std::mutex> Lock(thread_pool_mutex_);
    is_started_ = false;
    condition_variable_.notify_all();
    for (WorkThreadQueue::iterator it = work_thread_list_.begin(); it != work_thread_list_.end(); ++it) {
        (*it)->join();
        delete *it;
    }
    work_thread_list_.clear();
}

void ThreadPool::ThreadLoop() {
    while (true == is_started_) {
        Task NewTask = AcceptTask();
        if (NewTask) {
            NewTask();
        }
    }
}

void ThreadPool::AddTask(const Task& NewTask) {
    std::lock_guard<std::mutex> Lock(thread_pool_mutex_);
    task_list_.push_back(NewTask);
    condition_variable_.notify_one();
}

ThreadPool::Task ThreadPool::AcceptTask() {
    std::unique_lock<std::mutex> Lock(thread_pool_mutex_);
    // always use a while-loop, due to spurious wakeup
    while (task_list_.empty() && is_started_) {
        condition_variable_.wait(Lock);
    }
    Task NewTask;
    TaskList::size_type size = task_list_.size();
    if (!task_list_.empty() && is_started_) {
        NewTask = task_list_.front();
        task_list_.pop_front();
    }
    return NewTask;
}

原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/277809.html

(0)
上一篇 2022年7月30日
下一篇 2022年7月30日

相关推荐

发表回复

登录后才能评论