I have used std::mutex extensively in my codebase. However, one of the classes simply does not let me add a mutex to its instance variables list. I am instantiating the mutex quite simply using the following -
std::mutex myMutex;
I added the above statement to the private section of the class's members list.
I get the following error -
error C2280: 'std::mutex::mutex(const std::mutex &)' : attempting to reference a deleted function
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\mutex(113) : see declaration of 'std::mutex::mutex'
The class I am trying to add the mutex gets copied.
As I said, I have used mutex all over my codebase, so it is very strange that I cannot instantiate the mutex in one place while I can instantiate in all other places. Can you please tell me what is going wrong here?
You're adding a std::mutex
to a class with a copy constructor. Most likely, your class has a default copy constructor that is being used. The default copy constructor calls the copy constructor on each member in the class. But std::mutex
's copy constructor is deleted, by spec – it's a non-copyable type.