为什么std :: mutex在使用WIndows SOCKET的结构中使用时会创建C2248?

Xen*_*sis 5 c++ mutex visual-c++ c++11 visual-studio-2012

我使用结构来支持Windows SOCKET列表:

struct ConnectedSockets{
    std::mutex lock;
    std::list<SOCKET> sockets;
};
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它(Visual Studio 2012)时,我收到以下错误:

"错误C2248:std::mutex::operator =无法访问在类中声明的'私有'成员'std::mutex'"

有人知道如何解决这个问题吗?

K-b*_*llo 6

一个std::mutex是不可拷贝,所以你将需要实现operator=ConnectedScokets自己.

我认为你想保留mutex每个实例ConnectedSockets,所以这应该足够了:

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

    return *this;
}
Run Code Online (Sandbox Code Playgroud)