繁体   English   中英

CPP std :: thread尝试使用已删除的功能

[英]CPP std::thread attempt to use a deleted function

首先,我想说的是我已经对该主题进行了研究,但没有任何关系...

在Mac OS X上使用clang创建“ std :: thread时出错:“尝试使用已删除的功能” ))

Xcode 7:C ++线程错误:尝试使用已删除的函数

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,它将崩溃,即使您使用atomic_bool,它们也会全部报告回相同的内存位置,如果其中之一返回false,则该函数将丢失通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM