effective c++ 之std::thread 绑定类函数

effective c++ 之std::thread 绑定类函数

问题:想用std::thread新开线程去执行一个类中的函数,编译报错。

代码示例(手写仅供参考)

 1 class A {
 2 public:
 3     inline void start() {
 4         std::thread run_thread(&A::real_run);
 5         run_thread.join();    
 6     }
 7     inline void real_run() {
 8         std::cout << "real_run" << std::endl;
 9     }
10 }

以上代码会报错

解决方案:

1. 把想绑定的类函数设为static

    但是会引入新的问题,static方法不能使用类的非静态成员变量

    1.1 针对这一问题,解决方法如下: 给该静态成员函数传递this指针,通过this指针调用费静泰成员变量 

 1 class A {
 2 public:
 3     inline void start() {
 4         std::thread run_thread(&A::real_run, this);
 5         run_thread.join();    
 6     }
 7     inline void real_run(A *ptr) {
 8         std::cout << "real_run" << ptr->data << std::endl;
 9     }
10    private:
11         int data;
12 }

 

2. 更为优雅的方案(个人认为比较好的方法)

  使用std::bind绑定方法和this指针 传递给thread

 1 class A {
 2 public:
 3     inline void start() {
 4         std::thread run_thread(std::bind(&A::real_run, this));
 5         run_thread.join();    
 6     }
 7     inline void real_run() {
 8         std::cout << "real_run" << std::endl;
 9     }
10 }

 

posted on 2017-02-08 19:47  csw_trying  阅读(2398)  评论(0编辑  收藏  举报

导航