首先,我想说我已经对这个主题进行了研究,但没有任何相关...
(在 Mac OS X 上使用 clang 创建 std::thread 时出错:“尝试使用已删除的函数”)
(xcode - “尝试使用已删除的函数” - 这是什么意思?)
这是我的问题...:
铿锵错误:
/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);
如果有人可以帮助我,那就真的很有帮助了! :)
问候;)
要传递对线程的引用,您必须使用
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
。
它抱怨的已删除函数是 const 线程的已删除复制构造函数。
对于删除功能的问题,可以使用:
threads.emplace_back(
代替:
threads.push_back(
评论者还提到,该函数正在创建多个线程并向它们传递对同一布尔返回变量的引用。
如果不使用atomic_bool,它会崩溃,即使使用,它们也会报告回相同的内存位置,如果其中一个返回 false,则函数会错过通知。
我会回去读书。只需使用另一种语言 - Python!
或者使用 emplace_back
弗拉基米尔·米科夫 4044087744