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

2020-01-06 14:01:59于海丽
  • {  throw TC_ThreadPool_Exception("[TC_ThreadPool::KeyInitialize] pthread_key_create error", ret); 
  • }  } 
  •   /** 
  • * @brief 释放key  */ 
  • ~KeyInitialize()  { 
  • pthread_key_delete(TC_ThreadPool::g_key);  } 
  • };   
  • /**  * @brief 初始化key的控制 
  • */  static KeyInitialize g_key_initialize; 
  •   /** 
  • * @brief 数据key  */ 
  • static pthread_key_t g_key;   
  • protected:  /** 
  • * @brief 线程池中的工作线程  */ 
  • class ThreadWorker : public TC_Thread  { 
  • public:  /** 
  • * @brief 工作线程构造函数.  *  
  • * @ param tpool  */ 
  • ThreadWorker(TC_ThreadPool *tpool);   
  • /**  * @brief 通知工作线程结束 
  • */  void terminate(); 
  •   protected: 
  • /**  * @brief 运行 
  • */  virtual void run(); 
  •   protected: 
  • /**  * 线程池指针 
  • */  TC_ThreadPool * _tpool; 
  •   /** 
  • * 是否结束线程  */ 
  • bool _bTerminate;  }; 
  •   protected: 
  •   /** 
  • * @brief 清除  */ 
  • void clear();   
  • /**  * @brief 获取任务, 如果没有任务, 则为NULL. 
  • *  * @return TC_FunctorWrapperInterface* 
  • */  TC_FunctorWrapperInterface * get(ThreadWorker *ptw); 
  •   /** 
  • * @brief 获取启动任务.  * 
  • * @return TC_FunctorWrapperInterface*  */ 
  • TC_FunctorWrapperInterface * get();   
  • /**  * @brief 空闲了一个线程.