How to declare a thread inside a class which run a member function? I tried several approaches according to online search : this
std::thread t(&(this->deQRequest));
this
std::thread t([this]{ deQRequest(); });
this
std::thread t(&this::deQRequest, this);
or
std::thread t(&this::deQRequest, *this);
None of them works.
Then I tried the following code, it works:
std::thread spawn() {
return std::move(
std::thread([this] { this->deQRequest(); })
);
}
but my question is, why this
std::thread t([this]{ deQRequest(); });
doesn't work? it always reminds an error: "Explicit type is missing, 'int' assumed" and "expected a declaration" .
My deQRequest function is a member function in the same class, my class looks like this:
class sender{
public:
void deQRequest(){
//some execution code
};
private:
// here I try to declare a thread like this:std::thread t([this]{ deQRequest(); });
}