I'm trying to use mutex to synchronise the execution of several methods in different threads. I create a class Bar that contains a foo method and a mutex attribute. Then I want to run that foo method in a thread. I don't understand why it is a problem and the following code does not compile? And how to fix this? Any help appreciated.
#include <thread>
#include <mutex>
class Bar
{
public:
Bar (){};
void foo(){};
std::mutex m_;
};
int main(void)
{
Bar b;
std::thread t(&Bar::foo, b);
return 0;
}
I'm getting the following errors:
include/c++/7.3.0/thread:256:11: error: no matching constructor for initialization of '__decayed_tuple<void (Bar::*)(), Bar &>' (aka 'std::tuple<void (Bar::*)(), Bar>')
return { __decayed_tuple<_Callable, _Args...>{
and
include/c++/7.3.0/tuple:133:4: error: call to implicitly-deleted copy constructor of 'Bar'
: _M_head_impl(std::forward<_UHead>(__h)) { }