C++线程池的实现

文章展示了一个使用C++实现的线程池类ThreadPool,它创建指定数量的工作线程来执行任务。线程池接受任务并使用互斥锁和条件变量进行同步,确保在停止时正确处理未完成的任务。在主函数中,创建了一个线程池实例,并向其添加了多个SayHello任务。
#include <iostream>
#include<thread>
#include <queue>
#include <mutex>
#include <chrono>
#include <condition_variable>
#include <functional>
using namespace std;

#define NUM_THREADS 5

class ThreadPool {
public:
    explicit ThreadPool(size_t size):stop_(false) {
        // 创建线程池
        for(size_t i = 0; i < size; i++) {
            threads_.emplace_back([this] {
                while (true) {
                    unique_lock<mutex> lock(mutex_);
                    printf("wait");
                    condition_.wait(lock, [this] {return stop_ || !tasks_.empty();});
                    if(stop_ && tasks_.empty() ) {
                        return;
                    }
                    auto task = move(tasks_.front());
                    tasks_.pop();
                    lock.unlock();
                    task();
                }
            });
        }
    }
    template<class F>
    void addTask(F&& f) {
        printf("add");
        std::unique_lock<mutex> lock(mutex_);
        tasks_.emplace(forward<F>(f));
        condition_.notify_one();
    }
    ~ThreadPool() {
        printf("pool delete");
        std::unique_lock<std::mutex> lock(mutex_);
        stop_ = true;
        condition_.notify_all();
        for (auto& thread : threads_) {
            thread.join();
        }
    }
private:
    bool stop_;
    std::vector<std::thread> threads_;
    std::queue<std::function<void()>> tasks_;
    std::mutex mutex_;
    condition_variable condition_;
};
// 线程的运行函数
void say_hello(int num)
{
    printf("Hello Runoob!%d\n", num);
}
 

 
void SayHello(int num) {
    // std::cout << "Hello from thread " << num << std::endl;
    for(int i = 0; i < num; i ++) {
        cout << "循环" << num << endl;
    }
}
 
int main() {
    ThreadPool pool(16);
    for (int i = 0; i < 800; ++i) {
        pool.addTask([i] { SayHello(i); });
    }
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return 0;
}

C++线程池实现主要涉及任务队列、工作线程、任务提交接口和线程管理等部分,以下是详细的实现步骤与要点: ### 线程池设计 - **任务类型**:任务通常为可调用对象,像函数、Lambda表达式等,可借助`std::function`封装任务。 - **线程池管理**:线程池要有一个工作线程的集合,通过`std::thread`创建线程并管理其生命周期。 - **同步机制**:为保证线程池能安全访问任务队列,需使用同步机制,例如互斥锁(`std::mutex`)和条件变量(`std::condition_variable`)[^1]。 ### 核心功能 - **提交任务**:外部可通过`submit`函数将任务提交到线程池中。 - **工作线程**:线程池里的工作线程会持续从任务队列中获取任务并执行。 - **线程池销毁**:线程池在无需使用时要进行销毁,确保线程和资源得到正确释放[^1]。 ### 实现代码示例 下面为一个简单的C++线程池实现示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <functional> #include <atomic> class ThreadPool { public: ThreadPool(size_t numThreads) : stop(false) { for (size_t i = 0; i < numThreads; ++i) { threads.emplace_back([this] { while (true) { std::function<void()> task; { std::unique_lock<std::mutex> lock(this->queueMutex); this->condition.wait(lock, [this] { return this->stop ||!this->tasks.empty(); }); if (this->stop && this->tasks.empty()) return; task = std::move(this->tasks.front()); this->tasks.pop(); } task(); } }); } } ~ThreadPool() { { std::unique_lock<std::mutex> lock(queueMutex); stop = true; } condition.notify_all(); for (std::thread &thread : threads) { thread.join(); } } template<class F> void enqueue(F&& f) { { std::unique_lock<std::mutex> lock(queueMutex); if (stop) throw std::runtime_error("enqueue on stopped ThreadPool"); tasks.emplace(std::forward<F>(f)); } condition.notify_one(); } private: std::vector<std::thread> threads; std::queue<std::function<void()>> tasks; std::mutex queueMutex; std::condition_variable condition; std::atomic<bool> stop; }; // 使用示例 void task() { std::cout << "Task is running." << std::endl; } int main() { ThreadPool pool(4); for (int i = 0; i < 10; ++i) { pool.enqueue(task); } return 0; } ``` 在上述代码中,`ThreadPool`类的构造函数会创建指定数量的工作线程,这些线程会在一个无限循环里等待任务。`enqueue`函数用于将任务添加到任务队列,并且通知一个等待的线程。析构函数会停止线程池并等待所有线程完成工作。 ### 其他应用示例 在`dbproxy`服务中使用线程池的示例如下: ```cpp static CThreadPool g_thread_pool; int init_proxy_conn(uint32_t thread_num) { // 省略其余代码 g_thread_pool.Init(thread_num); } void CProxyConn::HandlePduBuf(uchar_t* pdu_buf, uint32_t pdu_len) { // 省略其余代码 CTask* pTask = new CProxyTask(m_uuid, handler, pPdu); g_thread_pool.AddTask(pTask); } ``` 还有一个简单的使用示例: ```cpp #include "SyncQueue.hpp" #include "ThreadPool.hpp" #include <thread> #include <iostream> void task1() { std::cout << "task1 start" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(3)); std::cout << "task1 finish" << std::endl; } void task2() { std::cout << "task2 start" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(5)); std::cout << "task2 finish" << std::endl; } void task3() { std::cout << "task3 start" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(8)); std::cout << "task3 finish" << std::endl; } int main() { MyThreadPool::ThreadPool thread_pool(3); thread_pool.AddTask(task1); thread_pool.AddTask(task2); thread_pool.AddTask(task3); std::this_thread::sleep_for(std::chrono::seconds(1)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥aigc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值