简体   繁体   中英

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?

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;
}

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, eg 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;
   }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM