手写blockque

#include <queue>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <iostream>

template <typename T>
class BlockQue {

public:
    ~BlockQue(){
        endBlock();
    }
    void addTask(T data){
        //lock?
        m_tasks.push(data);
        m_task_cv.notify_all();
    } 
    int getTask(T& data){
        std::unique_lock<std::mutex> lock{ m_lock };
        m_task_cv.wait(lock, [this]{
            return m_end.load() || !m_tasks.empty();
        });
        if (m_tasks.empty()){
            return -1;
        }
        data = std::move(m_tasks.front());
        m_tasks.pop();
        return 0;
    }
    void endBlock(){
        if (!m_end.load()){
            m_end.store(true);
            m_task_cv.notify_all();
        }
    }
private:
    std::atomic<bool>         m_end{ false };
    std::condition_variable m_task_cv;
    std::queue<T>     m_tasks;
    std::mutex         m_lock;
};
 

------------------------------------------------------------------------------

#include "blockque.hpp"
#include <thread>
#include <atomic>
#include <vector>
#include <iostream>
#include <Windows.h>

using namespace std;


class Test{
public:

    void test(const int thId){
        //while(m_run.load()){
        while(true){
            short val =0;
            int ret =  m_blockque[thId].getTask(val);
            if(ret !=0){
                break;
                //cout<<"end blockque"<<endl;
                //continue;
            }
            cout<<"thid["<<thId<<"]  ==>"<<val<<endl;
        }
    }
    
    Test(){
        m_thpool.emplace_back( make_shared<thread>(&Test::test, this, 0));
        m_thpool.emplace_back( make_shared<thread>(&Test::test, this, 1));
        m_thpool.emplace_back( make_shared<thread>(&Test::test, this, 2));
    }
    

    ~Test(){
        //if (m_run.load()){
        //    m_run.store(false);
            for(auto& val : m_blockque){
                cout<<"ending"<<endl;
                val.endBlock();
            }
            std::cout<<"===thread join======"<<std::endl;
            for(auto& pThread : m_thpool){
                if(pThread != nullptr && pThread->joinable()){
                    pThread->join();
                }
            }
            m_thpool.clear();
        //}
    }
    
    
    void testTask(){
        int i=0;
        for(auto& val : m_blockque){
            val.addTask(i);
            cout<<"input"<<i<<endl;
            ++i;
        }
        Sleep(1);
    }
private:
    BlockQue<short>     m_blockque[3];
    //std::atomic<bool>     m_run{ true };
    vector<shared_ptr<thread>>     m_thpool;
      
};

int main()
{
    Test test;
    //Sleep(10);
    test.testTask();
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

字正腔圆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值