void CThreadPool::Run(CJob* job,void* jobdata) { if(job==NULL) return; //assert(job!=NULL); //if the busy thread num adds to m_MaxNum,so we should wait if(GetBusyNum() == m_MaxNum) { printf(")))))))))))))))))All is busy((((((((((((((((((,wait!\n"); m_MaxNumCond.Wait(); } //if the threads num after creating is also below m_MaxNum //so we can create new idle threads,else we needn't create,just to use the idle thread //printf("GetBusyNum():%d\n",GetBusyNum()); //printf("m_IdleList.size():%d\n",m_IdleList.size()); //printf("m_InitNum:%d\n",m_InitNum); //printf("GetAllNum()+m_InitNum-m_IdleList.size():%d\n",GetAllNum()+m_InitNum-m_IdleList.size()); if(m_IdleList.size()<m_AvailLow) { if(GetAllNum()+m_InitNum-m_IdleList.size() < m_MaxNum ) { printf("m_IdleList.size() < m_AvailLow,so new thread should be created \n"); CreateIdleThread(m_InitNum-m_IdleList.size()); } else { CreateIdleThread(m_MaxNum-GetAllNum()); } } /* if(m_IdleList.size()<m_AvailLow && GetAllNum()+m_InitNum-m_IdleList.size()<m_MaxNum) { printf("m_IdleList.size() < m_AvailLow,so new thread should be created \n"); CreateIdleThread(m_InitNum-m_IdleList.size()); } else if(m_IdleList.size()<m_AvailLow && m_MaxNum-GetAllNum() < m_AvailLow) { CreateIdleThread(m_MaxNum-GetAllNum()); } */ CWorkerThread* idlethr = GetIdleThread(); if(idlethr !=NULL) { idlethr->m_WorkMutex.Lock(); MoveToBusyList(idlethr); idlethr->SetThreadPool(this); job->SetWorkThread(idlethr); //printf("Job is set to thread %d \n",idlethr->GetThreadID()); idlethr->SetJob(job,jobdata); } }
//create num idle thread and put them to idlelist void CThreadPool::CreateIdleThread(int num) { for(int i=0;i<num;i++) { CWorkerThread* thr = new CWorkerThread(); thr->SetThreadPool(this); AppendToIdleList(thr); m_VarMutex.Lock(); m_AvailNum++; m_VarMutex.Unlock(); //printf("===============Create A New Thread================\n"); thr->Start(); //begin the thread,the thread wait for job } }
CThreadPool::CThreadPool() { m_MaxNum = 50; m_AvailLow = 5; m_InitNum=m_AvailNum = 10 ; m_AvailHigh = 20; m_BusyList.clear(); m_IdleList.clear(); for(int i=0;i<m_AvailNum;i++) { //printf("create a thread\n"); CWorkerThread* thr = new CWorkerThread(); thr->SetThreadPool(this); AppendToIdleList(thr); thr->Start(); } }
CThreadPool::CThreadPool(int initnum) { assert(initnum>0 && initnum<=30); m_MaxNum = 30; m_AvailLow = initnum-10>0?initnum-10:3; m_InitNum=m_AvailNum = initnum ; m_AvailHigh = initnum+10; m_BusyList.clear(); m_IdleList.clear(); for(int i=0;i<m_AvailNum;i++) { //printf("create a thread\n"); CWorkerThread* thr = new CWorkerThread(); AppendToIdleList(thr); thr->SetThreadPool(this); thr->Start(); //begin the thread,the thread wait for job } }