c++11模板線程池

#pragma once

#include <vector>
#include <queue>
#include <thread>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>

namespace std
{
#define MAX_THREAD_NUM 256
class threadpool
{
public:
    inline threadpool(unsigned short size = 4) : stoped(false)
    {
        idlThrNum = size < 1 ? 1 : size;
        for (size = 0; size < idlThrNum; ++size)
        {
            pool.emplace_back([this] {
                while (!stoped)
                {
                    std::function<void()> task;
                    {
                        std::unique_lock<std::mutex> lock(m_lock);
                        cv_task.wait(lock, [this] { return stoped.load() || !tasks.empty(); });
                        if (!stoped.load() && !tasks.empty())
                        {
                            task = std::move(tasks.front());
                            tasks.pop();
                        }else{
                            continue;
                        }
                    }
                    idlThrNum--;
                    task();
                    idlThrNum++;
                }
                idlThrNum--;
            });
        }
    }

    inline ~threadpool()
    {
        stoped.store(true);
        cv_task.notify_all();
        for (std::thread &t : pool)
        {
            if (t.joinable())
            {
                t.join();
            }
        }
    }

    template <typename F, typename... Args>
    auto commit(F &&f, Args &&... args) -> std::future<decltype(f(args...))>
    {
        if (stoped.load())
        {
            throw std::runtime_error("commit on Threadpool is stopped");
        }

        using RetType = decltype(f(args...));
        auto task = std::make_shared<std::packaged_task<RetType()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
        std::future<RetType> future = task->get_future();
        {
            std::lock_guard<std::mutex> lock(m_lock);
            tasks.emplace([task] {
                (*task)();
            });
        }
        cv_task.notify_one();

        return future;
    }

    int idlCount() { return idlThrNum; }

private:
    using Task = std::function<void()>; //任務函數
    std::vector<std::thread> pool;      //線程池
    std::queue<Task> tasks;             //任務隊列
    std::mutex m_lock;                  //互斥鎖
    std::condition_variable cv_task;    //條件阻塞
    std::atomic<bool> stoped;           //是否關閉提交
    std::atomic<int> idlThrNum;         //空閒線程數量
};
} // namespace std

       當析構時如果還有未處理完的任務,會丟棄直接退出。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章