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

2020-01-06 14:01:59于海丽
  • , _bTerminate ( false)  { 
  • }   
  • void TC_ThreadPool ::ThreadWorker::terminate()  { 
  • _bTerminate = true;  _tpool->notifyT(); 
  • }   
  • void TC_ThreadPool ::ThreadWorker::run()  { 
  • //调用初始化部分  TC_FunctorWrapperInterface *pst = _tpool->get(); 
  • if(pst)  { 
  • try  { 
  • (*pst)();  } 
  • catch ( ... )  { 
  • }  delete pst; 
  • pst = NULL;  } 
  •   //调用处理部分 
  • while (! _bTerminate)  { 
  • TC_FunctorWrapperInterface *pfw = _tpool->get( this);  if(pfw != NULL) 
  • {  auto_ptr< TC_FunctorWrapperInterface> apfw(pfw); 
  •   try 
  • {  (*pfw)(); 
  • }  catch ( ... ) 
  • {  } 
  •   _tpool->idle( this); 
  • }  } 
  •   //结束 
  • _tpool->exit();  } 
  •   每个工作线程在刚开始时都会执行一下初始化操作,并进入一个无限循环的部分//调用处理部分 
  • while (! _bTerminate)  { 
  • TC_FunctorWrapperInterface *pfw = _tpool->get( this);  if(pfw != NULL) 
  • {  auto_ptr< TC_FunctorWrapperInterface> apfw(pfw); 
  •   try 
  • {  (*pfw)(); 
  • }  catch ( ... ) 
  • {  } 
  •   _tpool->idle( this); 
  • }  }