首先,我想说的是,我已经对这个问题进行了研究,但没有任何相关的.
(Error creating std::thread on Mac OS X with clang: "attempt to use a deleted function")
(Xcode 7: C++ threads ERROR: Attempting to use a deleted function)
(xcode - "attempt to use a deleted function" - what does that mean?)
我的问题是.
clang错误:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:347:5: error: attempt to use a deleted function
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);这是我的密码:
bool GenAI::loadAIs()
{
bool ret = true;
if (_nbThread > 1)
{
std::vector<std::thread> threads;
for (unsigned int i = 0; i < _nbThread; ++i)
threads.push_back(std::thread(static_cast<void (GenAI::*)(bool &, unsigned int)>(&GenAI::loadAIs), this, ret, i));
for (unsigned int i = 0; i < _nbThread; ++i)
threads[i].join();
}
else
loadAIs(ret, 0);
return ret;
}
// And the prototype of the function that i try to call
void GenAI::loadAIs(bool & ret, unsigned int iThread);如果有人能帮我,那会很有帮助的!:)
问候;)
发布于 2017-05-09 11:35:22
要传递对线程的引用,您必须使用std::reference_wrapper,这是您可以通过std::ref获得的。所以你的代码变成:
threads.emplace_back(static_cast<void (GenAI::*)(bool &, unsigned int)>(&GenAI::loadAIs),
this,
std::ref(ret),
i));注意:bool ret可能应该是std::atomic<bool> ret,或者应该通过线程拥有一个bool。否则,您可以对ret进行并发访问。
发布于 2017-05-09 09:20:18
它正在抱怨的已删除函数是const线程的已删除复制构造函数。
对于已删除的函数问题,可以使用:
threads.emplace_back(而不是:
threads.push_back(注释还提到的是,该函数正在创建多个线程,并向它们传递对同一个布尔返回变量的引用。
如果您不使用atomic_bool,它就会崩溃,即使使用了,它们也都会返回到相同的内存位置,如果其中一个返回false,函数就会错过通知。
https://stackoverflow.com/questions/43865718
复制相似问题