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 
ChildElement :: ChildElement() {}
ChildElement :: ~ChildElement() {}
void ChildElement ::Task(){/* task function */ std :: cout<< "TaskRunning...."<<std :: endl;}
#ifndef THREADPOOL_H_INCLUDED
#define THREADPOOL_H_INCLUDED
#include 
#include 
#include 
#include 
#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 
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;
            while (!tp->isShutdown);
            break;
            tp->overTaskMutex.lock();
            tp->busyList.pop_front();
            /* 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(&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 
#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 ;
}

测试结果
C++线程池简单示例_第1张图片



你可能感兴趣的:(线程池)