linux pthread 线程池,基于pthread的线程池,C++实现

然后是比较关键的MyThreadPool类

MyThreadPool.h

#ifndef MYTHREADPOOL_H_

#define MYTHREADPOOL_H_

#include "MyWorkThread.h"

#include "BaseJob.h"

#include

#include

#include

#include

#include

using namespace std;

class MyThreadPool {

friend class MyWorkThread;

private:

class MyThreadMutex{

private:

pthread_mutex_t _lock;

public:

MyThreadMutex(){pthread_mutex_init(&_lock,NULL);}

~MyThreadMutex(){pthread_mutex_destroy(&_lock);}

void Lock(){pthread_mutex_lock(&_lock);}

void Unlock(){pthread_mutex_unlock(&_lock);}

};

public:

MyThreadPool();

MyThreadPool(int initNum);

virtual ~MyThreadPool();

MyThreadMutex m_BusyMutex;

MyThreadMutex m_IdleMutex;

vector  m_BusyList; //Thread List

vector  m_IdleList; //Idle List

void    SetMaxNum(int maxnum){m_MaxNum = maxnum;}

int    GetMaxNum(void){return m_MaxNum;}

int    GetBusyNum(void){return m_BusyList.size();}

int  GetIdleNum(void){return m_IdleList.size();}

int  GetAllNum(void){return m_BusyList.size()+m_IdleList.size();}

void    SetInitNum(int initnum){m_InitNum = initnum;}

int    GetInitNum(void){return m_InitNum;}

void    TerminateAll(void);

void    Run(BaseJob* job, void* jobdata);

protected:

MyWorkThread* GetIdleThread(void);

void AppendToIdleList(MyWorkThread* jobthread);

void MoveToBusyList(MyWorkThread* idlethread);

void MoveToIdleList(MyWorkThread* busythread);

void CreateIdleThread(int num);

private:

unsigned int m_InitNum;

unsigned int m_MaxNum;  //the max value of the thread

};

#endif /* MYTHREADPOOL_H_ */

MyThreadPool.cpp

#include "MyThreadPool.h"

#include

MyThreadPool::MyThreadPool() {

// TODO Auto-generated constructor stub

m_MaxNum = 10;

m_InitNum = 5;

m_BusyList.clear();

m_IdleList.clear();

for(int i=0; i

MyWorkThread* thread = new MyWorkThread();

thread->SetThreadPool(this);

AppendToIdleList(thread);

thread->Create(thread);

}

}

MyThreadPool::MyThreadPool(int InitNum) {

assert(InitNum>=2 && InitNum<=20);

m_MaxNum = 10;

m_InitNum = InitNum;

m_BusyList.clear();

m_IdleList.clear();

for(int i=0; i

MyWorkThread* thread = new MyWorkThread();

thread->SetThreadPool(this);

AppendToIdleList(thread);

thread->Create(thread);

}

}

MyThreadPool::~MyThreadPool() {

// TODO Auto-generated destructor stub

}

MyWorkThread* MyThreadPool::GetIdleThread(void){

while(m_IdleList.size() <= 0){

usleep(10);

}

m_IdleMutex.Lock();

if(m_IdleList.size()>0){

MyWorkThread* thread = (MyWorkThread*)m_IdleList.front();

m_IdleMutex.Unlock();

return thread;

}

m_IdleMutex.Unlock();

return NULL;

}

//add a thread into idle list

void MyThreadPool::AppendToIdleList(MyWorkThread* thread){

m_IdleMutex.Lock();

m_IdleList.push_back(thread);

m_IdleMutex.Unlock();

}

//move the idle thread from idle list to busy list

void MyThreadPool::MoveToBusyList(MyWorkThread* idlethread){

printf("MoveToBusyList\n");

m_BusyMutex.Lock();

m_BusyList.push_back(idlethread);

m_BusyMutex.Unlock();

m_IdleMutex.Lock();

vector::iterator pos;

pos = find(m_IdleList.begin(), m_IdleList.end(), idlethread);

if(pos != m_IdleList.end()){

m_IdleList.erase(pos);

}

m_IdleMutex.Unlock();

}

//move the busy thread from busy list to idle list

void MyThreadPool::MoveToIdleList(MyWorkThread* busythread){

printf("MoveToIdleList\n");

m_IdleMutex.Lock();

m_IdleList.push_back(busythread);

m_IdleMutex.Unlock();

m_BusyMutex.Lock();

vector::iterator pos;

pos = find(m_BusyList.begin(), m_BusyList.end(), busythread);

if(pos != m_BusyList.end()){

m_BusyList.erase(pos);

}

m_BusyMutex.Unlock();

}

void MyThreadPool::CreateIdleThread(int num){

printf("CreateIdleThread:%d\n", num);

for(int i=0; i

MyWorkThread* thread = new MyWorkThread();

thread->SetThreadPool(this);

AppendToIdleList(thread);

thread->Create(thread);

}

}

void MyThreadPool::Run(BaseJob* job, void* jobdata){

assert(job != NULL);

while(GetBusyNum() >= m_MaxNum) {

usleep(10);

}

// if(GetAllNum() < m_MaxNum){

//  CreateIdleThread(m_MaxNum-GetAllNum());

// }

MyWorkThread* idlethread = GetIdleThread();

if(idlethread != NULL){

MoveToBusyList(idlethread);

idlethread->SetThreadPool(this);

job->SetWorkThread(idlethread);

idlethread->SetJob(job, jobdata);

}

}0b1331709591d260c1c78e86d0c51c18.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值