[英]Visual Studio 2012 error C2248 in std::thread
I have a problem with the Visual Studio 2012 implementation of the std::thread
class.我对
std::thread
类的 Visual Studio 2012 实现有问题。
Error C2248: "std::thread::thread": cannot access private member declared in class std::thread
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 line: 606
A.hpp: A.hpp:
class A{
public:
A();
~A();
private:
vector<thread> listOfThreads;
int numberOfProcessorCores;
int startUpWorkerThreads();
};
A.cpp: A.cpp:
int A::startUpWorkerThreads(){
if(numberOfProcessorCores <= 0) return 2; //Keine Angabe zur Anzahl der Prozessorkerne
if(listOfThreads.size() > 0) return 3; //Bereits initialisiertdefiniert
for(int i = 0; i < numberOfProcessorCores; i ++){
thread newThread(&TaskManagement::TaskManager::queueWorker);
listOfThreads.push_back(newThread);
}
return 0;
}
This is the part of my programm where the thread-class is used.这是我的程序中使用线程类的部分。
Does anybody know why this error occures?有谁知道为什么会发生这个错误?
The error is telling you that an operation is attempting to call std::thread
's copy constructor or assignment operator, both of which are deleted or private.该错误告诉您一个操作正在尝试调用
std::thread
的复制构造函数或赋值运算符,这两者都已删除或私有。 As an alternative, you can "move" a thread into the vector by pushing a temporary like this:作为替代方案,您可以通过推送这样的临时文件将线程“移动”到向量中:
listOfThreads.push_back(thread(&TaskManagement::TaskManager::queueWorker));
otherwise, you could call std::move
on your thread object, which leaves you with a thread object in the same state as a default constructed one (thanks to @JonathanWakely for pointing that out in comments).否则,你可以在你的线程对象上调用
std::move
,这会让你拥有一个与默认构造的线程对象处于相同状态的线程对象(感谢@JonathanWakely 在评论中指出这一点)。 In your case, there is no reason to create a thread and explicitly move it.在您的情况下,没有理由创建线程并显式移动它。
std::thread
没有复制构造函数,它是执行push_back
所需的,并且可能用于其他vector
操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.