Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::mutex create a C2248 when used in a struct with WIndows SOCKET?

I´m using a struct to support a list of Windows SOCKET´s:

struct ConnectedSockets{
    std::mutex lock;
    std::list<SOCKET> sockets;
};

When i try to compile this (Visual Studio 2012) i get the following error:

"Error C2248: std::mutex::operator = cannot access 'private' member declared in class 'std::mutex'"

Does anybody know how to fix this?

like image 665
Xenogenesis Avatar asked Jan 10 '13 17:01

Xenogenesis


2 Answers

A std::mutex is not copyable, so you will need to implement the operator= for ConnectedScokets yourself.

I presume you want to keep a mutex per instance of ConnectedSockets, so this should be enough:

ConnectedSockets& operator =( ConnectedSockets const& other )
{
    // keep my own mutex
    sockets = other.sockets; // maybe sync this?

    return *this;
}
like image 118
K-ballo Avatar answered Oct 24 '22 16:10

K-ballo


Root cause has nothing about sockets. std::mutex is not copyable, so compiler fails to generate default assignment operator (which is memberwise) for ConnectedSockets struct. You need to instruct compiler to delete assignment operator (declare it using = delete specifier) or implement it manually, e.g. ignoring mutex copying.

// To make ConnectedSockets explicitly non-copyable and get more reasonable
// compiler error in case of misuse
struct ConnectedSockets
{
   std::mutex lock; 
   std::list<SOCKET> sockets;
   ConnectedSockets& operator=(const ConnectedSockets&) = delete;
};

// To make ConnectedSockets copyable but keep mutex member intact
struct ConnectedSockets
{
   std::mutex lock; 
   std::list<SOCKET> sockets;

   ConnectedSockets& operator=(const ConnectedSockets& i_rhs)
   {
      // Need locking? Looks like it can be problem here because
      // mutex:lock() is not const and locking i_rhs wouldn't be so easy
      sockets = i_rhs.sockets;
      return *this;
   }
};
like image 20
Rost Avatar answered Oct 24 '22 16:10

Rost