【整理】C++ 11 std::thread如何调用类方法

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】C++ 11 std::thread如何调用类方法

参考自:

http://stackoverflow.com/questions/10998780/stdthread-calling-method-of-class?noredirect=1&lq=1

http://stackoverflow.com/questions/10673585/start-thread-with-member-function

C++ 11 std::thread如何调用本类里的方法呢?

示例代码demo1:

#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();
}

示例代码demo2(带参数传递):

class Test
{
public:
  void runMultiThread();
private:
  int calculate(int from, int to);
}

void Test::runMultiThread()
{
    std::thread t1(&Test::calculate, this,  0, 10);
    std::thread t2(&Test::calculate, this, 11, 20);
    t1.join();
    t2.join();
}
原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】C++ 11 std::thread如何调用类方法

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: