1
我有一个成员函数MyClass::doStuff(QString & str)
开始带有参数的成员函数在一个单独的线程
我试图调用该函数从一个线程是这样的:
std::thread thr(&MyClass::doStuff, this);
然而,这会导致错误:
/usr/include/c++/4.8.2/functional:1697: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (MyClass::*)(QString&)>(MyClass*)>’ typedef typename result_of<_Callable(_Args...)>::type result_type;
所以我试图给它的参数:
QString a("test");
std::thread thr(&MyClass::doStuff(a), this);
但是,导致此错误:lvalue required as unary ‘&’ operand
我怎么会去运行成员函数的参数,从一个单独的线程?
问题:因为'doStuff'索要参考,但它运行在另一个线程中,没有什么能阻止你删除'a',并且你最终会得到一个UB? – Ceros
@Ceros当然,如果你删除了一个别的东西正在使用的变量会有麻烦:) – Galik
在这种情况下'std :: ref'的目的是什么? – Ceros