I used to create a thread in this way
std::thread(&A::Func, this);
But I find there's another way
std::thread(&A::Func, std::ref(*this));
What's the difference between them?
In the context of launching a thread running a member function of a class A
, those calls are equivalent.
The first is like
void compiler_created_on_new_thread(A * a) { a->Func(); }
The second is like
void compiler_created_on_new_thread(A & a) { a.Func(); }
If instead A
were a namespace, they would be distinguishable
namespace A {
void Func(Thing *) { std::cout << "pointer"; }
void Func(Thing &) { std::cout << "reference"; }
}
The first would display "pointer" and the second "reference"