As I understand, the name of a function itself serves as a pointer to it.
Therefore, when I have a function, I can create a thread by simply passing its address to the thread constructor, like below:
void thread_function {
}
std::thread threadObj1(thread_function);
My confusion is while passing the address of a non-static member function to a thread. For example:
class ClassA
{
public:
void nonstatic_function()
{
}
};
ClassA instance;
std::thread threadObj2(ClassA::nonstatic_function, &instance);
Passing the address of such a function is done in two ways:
ClassA::nonstatic_function
&ClassA::nonstatic_function
Why is there an extra &
? If it is indeed needed, then how come the compiler does not complain even without it?