参考自:
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();
}
文章的脚注信息由WordPress的wp-posturl插件自动生成
微信扫一扫,打赏作者吧~

