c++11用类的成员函数创建线程

c++11用类的成员函数创建线程

当需要利用类成员函数( MyClass::thread_func )来创建子线程时,需如下码码:

t[i] = thread(std::mem_fn(&MyClass::thread_func), Object, args..);    

如果thread_func为static,则不用写object。否则需要,如主进程所调函数也为该类成员,则传入this指回自己。

代码如下 

#include<thread>
class MyTest 
{
public:
MyTest() {}
~MyTest() {}
void threadFunc() 
{
std::cout << "threadFunc" << std::endl;
}

};


int _tmain(int argc, _TCHAR* argv[]) 
{
MyTest Test;
std::thread t = std::thread(std::mem_fn(&MyTest::threadFunc), Test);
t.join();
getchar();
return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_23069315/article/details/80265758