I have a problem using std :: thread in my code:
Class Timer { ... public: void Start(bool Asynch = true) { if (IsAlive()) { return; } alive = true; repeat_count = call_number; if (Asynch) { t_thread = std::thread(&ThreadFunc, this); } else { this->ThreadFunc(); } } void Stop() { alive = false; t_thread.join(); } ... }
I get the following error:
error C2276: '&': illegal operation on the expression of a function of related members
t_thread is a private std :: thread instance class, ThreadFunc () is a private member function of the class that returns void;
I think I understand that there are 2 ways to send a member function to std :: thread, if this function is static, I would use t_thread = std :: thread (threadfunc); But I do not want ThreadFunc to be static, and do it as if it gives me an error.
I think I solved the problem by creating another function:
std::thread ThreadReturner() { return std::thread([=] { ThreadFunc(); }); } ... t_thread = ThreadReturner();
Thus, I do not get errors, but I do not understand why the first of them does not work.
Any help is appreciated.
My question looks like a duplicate, but there is only 1 difference, in the answer to another question std :: thread was used outside the class or implementation declaration, it was in main (), and in this case the definition of the area made sense to me, but not then. when std :: thread was called inside a class. This is the only difference that I saw and why I created this topic, sorry for the possible duplicate.
source share