错误 C2280:'std::thread::thread(const std::thread &)':尝试引用已删除的函数

Error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

本文关键字:thread std 引用 删除 函数 C2280 错误 const      更新时间:2023-10-16

我在创建使用C++11标准线程的VC++静态库时遇到问题。

我目前有两个类,我可以在开始的类(最后声明的)上声明并稍后定义一个线程。在这个阶段,代码只是一个套接字侦听器,然后创建另一个类的对象来处理接受的每个客户端。这些子对象应该创建并行数据捕获、编码和传输所需的线程。

问题是:如果我在我的另一个类上声明了一个std::线程,即使和我在开始类上一样,不管怎样,我在构建error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function [...]vcincludefunctional 1124 1时都会收到这个错误

我能够解决这个错误的唯一方法是简单地不在后一个类中声明std::thread对象,根据我希望它做的,这是不可能的…

我使用的是VS2013,我的来源是:

stdafx.h

#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <thread>
#include <iostream>
#include <vector>

StreamServer.h

#pragma once
#define DEFAULT_BUFLEN 65535
#define DEFAULT_PORT "5649"
class StreamServerClient
{
public:
bool* terminate;
//std::thread client;     //If I comment this line out, it builds just fine.
void DoNothing();
StreamServerClient(SOCKET clientSock, bool* ptTerm);
StreamServerClient();
~StreamServerClient();
};
class StreamServer
{
public:
bool terminate;
std::thread Listener;
std::vector<StreamServerClient> clients;
void CreateClient(SOCKET, bool*);
void Listen();
StreamServer();
~StreamServer();
};

StreamServer.cpp

#include "stdafx.h"
#include "StreamServer.h"
StreamServerClient::StreamServerClient(SOCKET clientSock, bool* ptTerm)
{
terminate = ptTerm;
//client = std::thread(&StreamServerClient::DoNothing, this);     //Same thing as the declaration
}
StreamServerClient::StreamServerClient()
{
*terminate = false;
//client = std::thread(&StreamServerClient::DoNothing, this);     //Same thing as the declaration
}
void StreamServerClient::DoNothing()
{
}
StreamServerClient::~StreamServerClient()
{
}
void StreamServer::Listen()
{
{...}
do {
clients.push_back(StreamServerClient::StreamServerClient(accept(listenSock, NULL, NULL), &terminate));
std::cout << "accepted a client!" << std::endl;
} while (!terminate);
}
StreamServer::StreamServer()
{
terminate = false;
Listener = std::thread(&StreamServer::Listen, this);
Listener.detach();
}
StreamServer::~StreamServer()
{
}
无法复制std::thread类型的对象。您最好只初始化成员初始值设定项列表中的对象:
class StreamServerClient
{
public:
bool* terminate;
std::thread client;
void DoNothing();
StreamServerClient(SOCKET clientSock, bool* ptTerm);
StreamServerClient(StreamServerClient&& other);
~StreamServerClient();
};
StreamServerClient::StreamServerClient(SOCKET clientSock, bool* ptTerm)
: terminate(ptTerm)
, client(std::thread(&StreamServerClient::DoNothing, this)) {
}
StreamServerClient::StreamServerClient(StreamServerClient&& other)
: terminate(other.terminate)
, client(std::move(other.client)) {
}

我提交了默认构造函数(请注意,您的版本不起作用,因为它试图为取消引用未初始化指针的结果分配一个值),而是添加了一个移动构造函数:当推回到std::vector<...>时,当提供看起来像临时的东西时,将调用此构造函数(即,临时的或看起来像临时的东西,例如,使用std::move())。

std::thread对象不可复制。当这条线被调用时:

clients.push_back(StreamServerClient::StreamServerClient(accept(listenSock, NULL, NULL), &terminate));

您创建的StreamServerClient对象需要添加到clients向量中,但唯一的方法是复制它,因为您的StreamServer对象没有定义移动构造函数。生成了StreamServerClient的默认复制构造函数,而C++11中的默认复制构造器所做的是,它调用所有数据成员的复制构造函数。

在这种情况下,它正在调用已删除的std::thread数据成员的复制构造函数。

std::threads无法复制的原因是std::thread对象对应于执行的线程。IMO,你应该重构你的代码。你可以为StreamServerClient拼凑一个移动构造函数,但我认为有更干净的解决方案,例如,用指向std::thread的指针替换std::thread,并用一个完全独立的调用初始化线程。

编辑:一般来说,在对象的构造函数中分支一个新线程是不明智的,因为这是一个过于精细的操作。(它可能会失败,然后你必须处理异常等)尽管C++纯粹主义者可能会不同意我的观点。

编辑:有愚蠢的术语。