深入解析C++编程中线程池的使用

2020-01-06 14:01:59于海丽
  • thr->Start(); //begin the thread,the thread wait for job   
  • }   
  • }   
  •    
  • void CThreadPool::Run(CJob* job,void* jobdata)   
  • {   
  • assert(job!=NULL);   
  •    
  • //if the busy thread num adds to m_MaxNum,so we should wait   
  • if(GetBusyNum() == m_MaxNum)   
  • m_MaxNumCond.Wait();   
  •    
  • if(m_IdleList.size()<m_AvailLow)   
  • {   
  • if(GetAllNum()+m_InitNum-m_IdleList.size() < m_MaxNum )   
  • CreateIdleThread(m_InitNum-m_IdleList.size());   
  • else   
  • 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);   
  • }