Example #1
0
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);
    }
}
Example #2
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::Init () {
  //fprintf(stdout, "Enter WelsThreadPool Init\n");

  CWelsAutoLock  cLock (m_cLockPool);

  m_cWaitedTasks = new CWelsCircleQueue<IWelsTask>();
  m_cIdleThreads = new CWelsCircleQueue<CWelsTaskThread>();
  m_cBusyThreads = new CWelsList<CWelsTaskThread>();
  if (NULL == m_cWaitedTasks || NULL == m_cIdleThreads || NULL == m_cBusyThreads) {
    return WELS_THREAD_ERROR_GENERAL;
  }

  for (int32_t i = 0; i < m_iMaxThreadNum; i++) {
    if (WELS_THREAD_ERROR_OK != CreateIdleThread()) {
      return WELS_THREAD_ERROR_GENERAL;
    }
  }

  if (WELS_THREAD_ERROR_OK != Start()) {
    return WELS_THREAD_ERROR_GENERAL;
  }

  return WELS_THREAD_ERROR_OK;
}