C++ Thread类传入某类的函数作为参数时,要在随后传入该类的对象

std::thread 调用类的成员函数需要传递类的一个对象作为参数:

#include <thread>
#include <iostream>

class bar {
public:
  void foo() {
    std::cout << "hello from member function" << std::endl;
  }
};

int main()
{
  std::thread t(&bar::foo, bar());
  t.join();
}

如果是在类的成员函数中处理thread,传入 this 即可,如:

std::thread spawn() {
    return std::thread(&blub::test, this);
  }

猜你喜欢

转载自blog.csdn.net/hai008007/article/details/82422344
今日推荐