我试图从我的类中生成一个线程,并且该线程在我的类中执行特定的方法。代码如下所示:
class ThreadClass{
int myThread(int arg){
// do something
}
void createThread(){
thread t = thread(myThread,10);
}
} ;
这段代码在编译时会抛出一个错误
std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ThreadClass::*)(int), _Args = {int}]
no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (ThreadClass::*&&)(int)’
我不确定这里的实际错误是什么。有人可以帮我弄这个吗?
Thanks.
问题是没有对象就无法调用成员函数。提供一个指向this
以便使用当前对象:
thread t(&ThreadClass::myThread, this, 10);
您可以使用任何实例ThreadClass
对象,但就你而言,似乎this
是正确的做法。
注意:请记住,您需要对创建的线程的引用,以便您可以执行join()
稍后的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)