c++ - Starting a member function with arguments in a separate thread -


i have member function myclass::dostuff(qstring & str)

i trying call function thread this:

std::thread thr(&myclass::dostuff, this); 

however, results in error:

/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;

so i've attempted give argument:

qstring a("test"); std::thread thr(&myclass::dostuff(a), this); 

however, results in error: lvalue required unary ‘&’ operand

how go running member function argument, separate thread?

just add arguments thread's constructor:

qstring a("test"); std::thread thr(&myclass::dostuff, this, a); 

as function accepts reference should use std::ref() this:

myclass::dostuff(qstring& str) { /* ... */ }  // ...  qstring a("test"); std::thread thr(&myclass::dostuff, this, std::ref(a)); // wrap references 

Comments