Ejemplo n.º 1
0
	void SmtThreadPool::Run(SmtJob* pJob,void* pJobdata)
	{
		assert(pJob!=NULL); 
	
		if(GetBusyNum() == m_unMaxNum) 
			m_MaxNumCond.Wait(); 

		if(m_vIdleThreads.size()<m_unAvailIdleLow) 
		{ //负载过重
			if(GetAllNum()+m_unNormalIdleNum-m_vIdleThreads.size() < m_unMaxNum ) 
				CreateThread(m_unNormalIdleNum-m_vIdleThreads.size()); 
			else 
				CreateThread(m_unMaxNum-GetAllNum()); 
		}
		else if (m_vIdleThreads.size() > m_unAvailIdleHigh)
		{//负载过轻
			DeleteThread(m_vIdleThreads.size() - m_unNormalIdleNum); 
		}

		SmtWorkThread*  pIdleThread = GetIdleThread(); 

		if(pIdleThread !=NULL) 
		{ 
			pIdleThread->m_WorkCSLock.Lock(); 

			MoveToBusyList(pIdleThread); 

			pIdleThread->SetThreadPool(this); 

			pJob->SetWorkThread(pIdleThread); 
			pIdleThread->SetJob(pJob,pJobdata); 
		} 
	}
Ejemplo n.º 2
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);
    }
}