Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1680107
  • 博文数量: 206
  • 博客积分: 1450
  • 博客等级: 上尉
  • 技术积分: 2285
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-11 11:38
个人简介

学习永无止境!!

文章分类

全部博文(206)

文章存档

2022年(5)

2019年(3)

2018年(8)

2017年(32)

2016年(7)

2015年(13)

2014年(44)

2013年(24)

2011年(6)

2010年(17)

2009年(46)

2008年(1)

分类: C/C++

2016-12-18 11:35:28

经过common的学习之后,然后这个InternalThread类实际上就是boost库的thread的封装,然后对线程进行控制和使用。废话不多啰嗦

看看头文件:


  1. class InternalThread {  
  2.  public:  
  3.   // 构造函数和析构函数  
  4.   InternalThread() : thread_() {}  
  5.   virtual ~InternalThread();  
  6.   
  7.   /** 
  8.    * Caffe's thread local state will be initialized using the current 
  9.    * thread values, e.g. device id, solver index etc. The random seed 
  10.    * is initialized using caffe_rng_rand.   
  11.    *  caffe的线程局部状态将会使用当前线程值来进行初始化,当前的线程的值有设备id,solver的编号、随机数种子等 
  12.    */  
  13.   void StartInternalThread();  
  14.   
  15.   /** Will not return until the internal thread has exited. */  
  16.   // 是否知道线程退出才返回  
  17.   void StopInternalThread();  
  18.   // 线程是否已经起来了  
  19.   bool is_started() const;  
  20.   
  21.  protected:  
  22.   /* Implement this method in your subclass 
  23.       with the code you want your thread to run. */  
  24.   // 定义了一个虚函数,要求继承该类的必须要实现之  
  25.   virtual void InternalThreadEntry() {}  
  26.   
  27.   /* Should be tested when running loops to exit when requested. */  
  28.   // 在当请求退出的时候应该调用该函数  
  29.   bool must_stop();  
  30.   
  31.  private:  
  32.   void entry(int device, Caffe::Brew mode, int rand_seed, int solver_count,  
  33.       bool root_solver);  
  34.   // 内部的成员变量  
  35.   shared_ptrthread> thread_;  
  36. };  
  37.   
  38. }  // namespace caffe  

实现部分:



  1. namespace caffe {  
  2. // 析构函数,调用停止内部线程函数  
  3. InternalThread::~InternalThread() {  
  4.   StopInternalThread();  
  5. }  
  6.   
  7. // 测试线程是否起来  
  8. bool InternalThread::is_started() const {  
  9.   return thread_ && thread_->joinable(); // 首先thread_指针不能为空,然后该线程是可等待的(joinable)  
  10. }  
  11.   
  12. bool InternalThread::must_stop() {  
  13.   //  if interruption has been requested for the current thread, false otherwise. 见boost的doc  
  14.   return thread_ && thread_->interruption_requested();  
  15. }  
  16.   
  17. // 初始化工作,然后  
  18. void InternalThread::StartInternalThread() {  
  19.   CHECK(!is_started()) << "Threads should persist and not be restarted.";  
  20.   
  21.   int device = 0;  
  22. #ifndef CPU_ONLY  
  23.   CUDA_CHECK(cudaGetDevice(&device));  
  24. #endif  
  25.   Caffe::Brew mode = Caffe::mode();  
  26.   int rand_seed = caffe_rng_rand();  
  27.   int solver_count = Caffe::solver_count();  
  28.   bool root_solver = Caffe::root_solver();  
  29.   
  30.   try {// 然后重新实例化一个thread对象给thread_指针,该线程的执行的是entry函数  
  31.     thread_.reset(new boost::thread(&InternalThread::entry, this, device, mode,  
  32.           rand_seed, solver_count, root_solver));  
  33.   } catch (std::exception& e) {  
  34.     LOG(FATAL) << "Thread exception: " << e.what();  
  35.   }  
  36. }  
  37.   
  38. // 线程所要执行的函数  
  39. void InternalThread::entry(int device, Caffe::Brew mode, int rand_seed,  
  40.     int solver_count, bool root_solver) {  
  41. #ifndef CPU_ONLY  
  42.   CUDA_CHECK(cudaSetDevice(device));  
  43. #endif  
  44.   Caffe::set_mode(mode);  
  45.   Caffe::set_random_seed(rand_seed);  
  46.   Caffe::set_solver_count(solver_count);  
  47.   Caffe::set_root_solver(root_solver);  
  48.   
  49.   InternalThreadEntry();  
  50. }  
  51.   
  52. // 停止线程  
  53. void InternalThread::StopInternalThread() {  
  54.   if (is_started()) {// 如果线程已经开始  
  55.     thread_->interrupt();// 那么打断  
  56.     try {  
  57.       thread_->join();// 等待线程结束  
  58.     } catch (boost::thread_interrupted&) {//如果被打断,啥也不干,因为是自己要打断的^_^  
  59.     } catch (std::exception& e) {// 如果发生其他错误则记录到日志  
  60.       LOG(FATAL) << "Thread exception: " << e.what();  
  61.     }  
  62.   }  
  63. }  
  64.   
  65. }  // namespace caffe  
阅读(1392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~