我是初学者,我使用的是 Visual Studio C++ 2015。我总是看到这样的线程创建:
thread nameThread(functionName);
问题是:如何创建使用一个对象的函数成员的线程,而这个函数需要一个指向另一个对象的属性的指针?像这样:
if(something == true){
thread nameThread( object.function( &anotherObject.attribute ) );
}
.
.
.
nameThread.join();
非常感谢。
最佳答案
在过去,我们不得不使用黑魔法来做到这一点。但是 std::thread
足够聪明,可以为我们“自动”实现它提供重载:
std::thread nameThread(
&theTypeOfObject::function, // function to invoke
&object, // first arg (implicit "this" pointer)
&anotherObject.attribute // second arg (first one you see)
);
关于c++ - 在 C++ 中使用对象的函数创建线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809489/