cond_thread

#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <unistd.h>
#include <condition_variable>
#include <iostream>

template <typename T>
class cond_thread {
    typedef void (*FuncPtr)(T data, const int thId);

public:
    cond_thread(FuncPtr funcPtr, const int thId):m_funPtr(funcPtr),m_thId(thId){
        std::cout<<"===construct======"<<std::endl;
        m_pThread = std::make_shared<std::thread>(&cond_thread::run,this);
    }
    ~cond_thread(){
        if (m_run.load()){
            m_run.store(false);
            m_task_cv.notify_all();
            if(m_pThread != nullptr && m_pThread->joinable()){
                m_pThread->join();
            }
        }
        std::cout<<"===distruct======"<<std::endl;
    }
    
    void addTask(T data){
        m_tasks.push(data);
        m_task_cv.notify_all();
    } 
protected:
    void run(){
        while(m_run.load()){
            std::unique_lock<std::mutex> lock{ m_lock };
            m_task_cv.wait(lock, [this]{
                return !m_run.load() || !m_tasks.empty();
            });
            if (!m_run.load() && m_tasks.empty()){
                return;
            }
            T task = std::move(m_tasks.front());
            m_tasks.pop();
            if(m_funPtr != nullptr){
                m_funPtr(task, m_thId);
            }
        }
    }
private:
    const int                     m_thId;
    std::atomic<bool>             m_run{ true };
    std::shared_ptr<std::thread> m_pThread;
    std::condition_variable m_task_cv;
    std::queue<T>     m_tasks;
    std::mutex         m_lock;
    FuncPtr         m_funPtr;
};
 

-----------------上述代码待完成 thread 是怎么把成员函数作为--------------------------------

    template<typename _Callable, typename... _Args, typename = _Require<__not_same<_Callable>>>
      explicit
      thread(_Callable&& __f, _Args&&... __args)
      {

        auto __depend = reinterpret_cast<void(*)()>(&pthread_create);

        using _Invoker_type = _Invoker<__decayed_tuple<_Callable, _Args...>>;

        _M_start_thread(_S_make_state<_Invoker_type>( std::forward<_Callable>(__f), std::forward<_Args>(__args)...), __depend);
      }

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

#include "cond_thread.hpp"
#include <vector>
#include <Windows.h>

using namespace std;

void test(short data, const int thId){
    cout<<thId<<"=>"<<data<<endl;
    fflush(stdout);
}


class Test{
public:

    Test(){
        m_thpool.emplace_back( new cond_thread<short>(test, 0));
        m_thpool.emplace_back( new cond_thread<short>(test, 1));
        m_thpool.emplace_back( new cond_thread<short>(test, 2));
    }
    void testTask(){
        int i=0;
        for(auto& val : m_thpool){
            val->addTask(i);
            //cout<<"input"<<i<<endl;
            ++i;
        }
        Sleep(1);
    }
    ~Test(){
        for(auto& val : m_thpool){
            delete val;
        }
        m_thpool.clear();
    }
private:
    vector<cond_thread<short>*> m_thpool;
};

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

字正腔圆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值