栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++11 线程池 实现,已部署~~

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++11 线程池 实现,已部署~~

每日一歌,分享好心情: 卡斯特梅的雨季

c++11 实现的线程池,项目已用,Lets go
用到了很多c11的语法,大家一起学习

一、实现
#ifndef _THREAD_POOL_HPP_
#define _THREAD_POOL_HPP_

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class ThreadPool {
public:
    ThreadPool(int num_threads = 4);
    ThreadPool(const ThreadPool&) = delete;
    ThreadPool() = delete;
    ThreadPool& operator=(const ThreadPool&) = delete;
    ThreadPool& operator=(ThreadPool&&) = delete;

    
    template   
    auto AddTask(F&& f, Args&&... args) -> std::future;  

    ~ThreadPool();

private:
    std::vector _worker_threads;           
    bool _stop = false;                                 
    std::queue> _tasks_queue;     
    std::mutex _task_queue_mutex;                       
    std::condition_variable _condition;                 
};

ThreadPool::ThreadPool(int num_threads) 
    : _stop(false) {
    if (num_threads < 1) {
        throw std::runtime_error("ThreadPool: num_threads less than one");
    }

    
    for (int i=0; i
        _worker_threads.emplace_back(
            std::thread(
                [this]{
                    while(true) {
                        std::function task; 
                        {
                            std::unique_lock lock(this->_task_queue_mutex);     
                            
                            this->_condition.wait(lock, [this]{return this->_stop || !this->_tasks_queue.empty(); });
                            if (this->_stop && this->_tasks_queue.empty()) return;
                            task = std::move(this->_tasks_queue.front());
                            this->_tasks_queue.pop();
                        }
                        
                        task();
                    }
                }
            )
        );
    }
}

ThreadPool::~ThreadPool() {
    {
        std::unique_lock lock(_task_queue_mutex);
        _stop = true;
    }
    
    _condition.notify_all();
    for (std::thread &worker : _worker_threads) {
        worker.join();
    }
}


template 
auto ThreadPool::AddTask(F&&f, Args&&...args) -> std::future {
    using return_type = decltype(f(args...));
    
    auto task_ptr = std::make_shared>(
        std::bind(std::forward(f), std::forward(args)...));
    
    
    std::function warpper_func = [task_ptr]() { (*task_ptr)(); };
    {
        std::unique_lock lock(_task_queue_mutex);
        if (_stop) {
            throw std::runtime_error("ThreadPool is Stopped!");
        }
        _tasks_queue.emplace(warpper_func);
    }
    
    _condition.notify_one();

    
    return task_ptr->get_future();
}


#endif
二、测试代码
#include "threadpool.hpp"
#include 
#include 
#include 
using namespace std;


int main(int argc, char* argv[]) {
    
    int pool_size = 5;
    int task_num = 6;

    ThreadPool threadpool(pool_size);
    vector> resVec;
    
    for (int i=0; i
        resVec.emplace_back(
            threadpool.AddTask(
                [i] {
                    cout << "hello " << endl;
                    this_thread::sleep_for(chrono::seconds(1));
                    return i;
                }
            )
        );
    }

    
    for (auto&& result: resVec) {
        cout << result.get() << " ";
    }
    cout << endl;

    return  0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862578.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号