C++线程池简单示例

本线程池只写架构,任务可自行添加

抽象类接口

#ifndef BASEELEMENT_H_INCLUDED
#define BASEELEMENT_H_INCLUDED
class BaseElement{
public:
    BaseElement();
    ~BaseElement();
     virtual void Task() = 0 ;
private:
};
#include "BaseElement.h"
BaseElement :: BaseElement() {}
BaseElement :: ~BaseElement(){}

任务接口

#ifndef CHILDELEMENT_H_INCLUDED
#define CHILDELEMENT_H_INCLUDED
#include "BaseElement.h"
class ChildElement : public BaseElement{
public:
    ChildElement();
    ~ChildElement();
     void Task();
private:
};
#endif // CHILDELEMENT_H_INCLUDED
#include "ChildElement.h"
#include <iostream>
ChildElement :: ChildElement() {}
ChildElement :: ~ChildElement() {}
void ChildElement ::Task(){/* task function */ std :: cout<< "TaskRunning...."<<std :: endl;}
#ifndef THREADPOOL_H_INCLUDED
#define THREADPOOL_H_INCLUDED
#include <thread>
#include <mutex>
#include <condition_variable>
#include <list>
#include "BaseElement.h"
using namespace std ;
//class BaseElement ;
class ThreadPool{
public :
    ThreadPool( int  threadMaxNumber = 5);
    ~ThreadPool();
     bool InsertTask( BaseElement * be);
     bool UnloadThreadPool();
     int GetBusyThreadNumber() const ;
private:
    static void ThreadFunction( ThreadPool * tp);
private:
    list <BaseElement *> taskList;
    list <BaseElement *> busyList;
    thread *pThreadPool;
    mutex waitThreadMutex ;
    mutex popTaskMutex ;
    mutex overTaskMutex ;
    condition_variable  waitSignal;
    int threadMaxValue;
    bool isThreadMaxValue;
    bool isShutdown;
};
#endif // THREADPOOL_H_INCLUDED
#include"ThreadPool.h"
#include <iostream>
ThreadPool :: ThreadPool( int threadMaxNumber){
    threadMaxValue = threadMaxNumber;
    isShutdown = false ;
    if( threadMaxValue < 1 ){
        isThreadMaxValue = false ;
    }
    else{
        isThreadMaxValue = true ;
        pThreadPool = new thread[threadMaxNumber];
        for (int i = 0; i < threadMaxNumber; i++) {
            pThreadPool[i] = thread(ThreadFunction, this);
        }
    }
}
ThreadPool :: ~ThreadPool(){
    UnloadThreadPool();
}
 void ThreadPool :: ThreadFunction( ThreadPool * tp){
        while(tp->isThreadMaxValue){
            while( tp->taskList.size() == 0 && !tp->isShutdown){
                unique_lock<mutex> lock(tp->waitThreadMutex);
                tp->waitSignal.wait(lock);
            }
            if(tp->isShutdown){
                break ;
            }
            tp->popTaskMutex.lock();
            BaseElement * pbe = tp->taskList.front();
            tp->taskList.pop_front();
            tp->popTaskMutex.unlock();
            pbe->Task();
            cout << "ThreadID: " << this_thread::get_id() << endl;
            tp->overTaskMutex.lock();
            tp->busyList.remove(pbe);
            /* dispose over thread event  */
            delete pbe;
            tp->overTaskMutex.unlock();
        }
}
int ThreadPool  :: GetBusyThreadNumber() const {
    return busyList.size();
}
bool ThreadPool :: InsertTask(BaseElement * be){
    unique_lock<mutex> lock(waitThreadMutex);
    if(GetBusyThreadNumber() >= threadMaxValue || !isThreadMaxValue){
        return false ;
    }
    taskList.push_back(be);
    busyList.push_back(be);
    waitSignal.notify_one();
    return true ;
}
bool ThreadPool ::UnloadThreadPool(){
     if(!isThreadMaxValue || isShutdown){
         return false ;
     }
     waitSignal.notify_all();
     isShutdown = true ;
     for (int i = 0; i < threadMaxValue; i++) {
         pThreadPool[i].join();
         //delete[]static_cast<void*>(&pThreadPool[i]);
     }
     if(GetBusyThreadNumber() > 0){
         BaseElement* pbe;
         for (auto it = busyList.begin(); it != busyList.end(); ++it) {
             pbe = *it;
             cout << "ElementAddress: " << pbe << endl;
             delete pbe;
         }
        busyList.clear();
        taskList.clear();
        pbe = nullptr;
     }
     delete  []pThreadPool;
     return true ;
}
#include <iostream>
#include "ThreadPool.h"
//#include "BaseElement.h"
#include "ChildElement.h"
int main(){
    ThreadPool * tp = new ThreadPool(30);
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    tp->InsertTask(new ChildElement());
    this_thread::sleep_for(chrono::milliseconds(1000));
    delete tp;
    //cin.get();
    return 0 ;
}

测试结果
在这里插入图片描述



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值