c++多线程+自定义线程池

2023-06-10  本文已影响0人  c之气三段

https://blog.csdn.net/weixin_42193704/article/details/113920419

.hpp

#ifndef TASKTHREADPOOL_H
#define TASKTHREADPOOL_H

#include <iostream>
#include <thread>
#include <chrono>
#include <functional>
#include<queue>
#include<vector>
using namespace std;
enum ThreadStatus
{
    Vacant,//<<<空闲的
    Busy//<<<正在执行任务
};
template<typename T>
class TaskThreadPool
{
public:
    TaskThreadPool()
    {
       m_threadStatusVec.resize(1,ThreadStatus::Vacant);
    }
    /**
     * @brief addTask 添加一个任务
     * @param t 基本类型或结构体
     */
    void addTask(T t)
    {
        m_queue.push(t);
    }
    /**
     * @brief setFuction 设置任务方式
     * @param function 匿名函数
     */
    void setFuction(std::function<void(T,bool&)> function)
    {
        m_function = function;
    }
    /**
     * @brief setExpectThreadNum 开辟线程
     * @param expectThreadNum  期望的线程数
     * @return
     */
    unsigned int setExpectThreadNum(unsigned int expectThreadNum = 1)
    {
        unsigned int num;
        if(expectThreadNum < (m_maxSize-2))
        {
            num = expectThreadNum;
        }
        else {
            num = m_maxSize-2;
            if(num<1)
            {
                num = 1;
            }
        }
        m_currThreadNum = num;
        m_threadStatusVec.resize(m_currThreadNum,ThreadStatus::Vacant);
        return num;
    }
    void run()
    {
        m_stop = false;
        thread _thread([=](){
            while(!m_stop)
            {
                if(!m_queue.empty())
                {
                    for (size_t i = 0;i<m_currThreadNum;i++) {
                        if(m_threadStatusVec[i]==ThreadStatus::Vacant)
                        {
                            if(!m_queue.empty())
                            {
                              T t =  m_queue.front();
                              m_queue.pop();
                              std::thread currTread([=]()
                              {
                                  m_threadStatusVec[i] = ThreadStatus::Busy;
                                  m_function(t,m_stop);
                                  m_threadStatusVec[i] = ThreadStatus::Vacant;
                              });
                              currTread.detach();
                            }
                        }
                    }
                }
                else
                {
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                }
            }
        });
        _thread.detach();
    }
    void stop()
    {
        m_stop = true;
    }
private:
    function<void(T,bool&)> m_function;
    queue<T>m_queue;
    unsigned int m_maxSize = std::thread::hardware_concurrency();
    unsigned int m_currThreadNum =1;
    vector<ThreadStatus>m_threadStatusVec;
    bool m_stop = false;
};

#endif // TASKTHREADPOOL_H

    TaskThreadPool<int> m_TaskThreadPool;

    m_TaskThreadPool.addTask(5);
    m_TaskThreadPool.addTask(2);
    m_TaskThreadPool.setExpectThreadNum(1);
    m_TaskThreadPool.setFuction([](int a,bool& isStop){
            qDebug()<<a;
            qDebug()<<isStop;//随时停止线程
    });
    m_TaskThreadPool.run();

cout:
5
false
2
false
上一篇 下一篇

猜你喜欢

热点阅读