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

2020-01-06 14:01:59于海丽
  • }   
  • int ret = pthread_setspecific( g_key, ( void *)p);  if(ret != 0) 
  • {  throw TC_ThreadPool_Exception ("[TC_ThreadPool::setThreadData] pthread_setspecific error", ret); 
  • }  } 
  •   TC_ThreadPool ::ThreadData * TC_ThreadPool::getThreadData () 
  • {  return ( ThreadData *) pthread_getspecific( g_key); 
  • }   
  • void TC_ThreadPool::setThreadData( pthread_key_t pkey, ThreadData *p)  { 
  • TC_ThreadPool:: ThreadData *pOld = getThreadData(pkey);  if(pOld != NULL && pOld != p) 
  • {  delete pOld; 
  • }   
  • int ret = pthread_setspecific(pkey, ( void *)p);  if(ret != 0) 
  • {  throw TC_ThreadPool_Exception ("[TC_ThreadPool::setThreadData] pthread_setspecific error", ret); 
  • }  } 
  •   TC_ThreadPool ::ThreadData * TC_ThreadPool::getThreadData( pthread_key_t pkey) 
  • {  return ( ThreadData *) pthread_getspecific(pkey); 
  • }   
  • TC_ThreadPool::TC_ThreadPool()  : _bAllDone ( true) 
  • {  } 
  •   TC_ThreadPool::~TC_ThreadPool() 
  • {  stop(); 
  • clear();  } 
  •   void TC_ThreadPool::clear() 
  • {  std::vector< ThreadWorker *>::iterator it = _jobthread. begin(); 
  • while(it != _jobthread. end())  { 
  • delete (*it);  ++it; 
  • }   
  • _jobthread. clear();  _busthread. clear(); 
  • }   
  • void TC_ThreadPool::init( size_t num)  { 
  • stop();   
  • Lock sync(* this);   
  • clear();   
  • for( size_t i = 0; i < num; i++)  { 
  • _jobthread. push_back( new ThreadWorker( this));  }