1

First of all, I want to say that i already made researches on the subject, but nothing relevant...

(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?)

And here's my issue...:

clang error:

/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))...);

And that's my code:

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);

If some one could help me that'd be really helpful ! :)

Regards ;)

2
  • 3
    It should be something like std::ref(ret), and you should have one bool by thread or use std::atomic<bool>... Commented May 9, 2017 at 9:43
  • Thanks you very much, it was the std::ref(ret) that didnt compile... If you write it as an answer i'll be able to close this ticket :) Commented May 9, 2017 at 11:21

3 Answers 3

3

To pass reference to thread, you have to use std::reference_wrapper, that you can obtain with std::ref. So your code becomes:

threads.emplace_back(static_cast<void (GenAI::*)(bool &, unsigned int)>(&GenAI::loadAIs),
                     this,
                     std::ref(ret),
                     i));

Note: bool ret should probably be std::atomic<bool> ret, or should should have one bool by thread. Else you may have concurrent access on ret.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you a lot ! :) But it still works with vector::push_back();
I used emplace_back as it makes shorter line (and avoid one (elided?) move).
0

The deleted function that it is complaining about is a deleted copy constructor for const thread.

For the deleted function problem, you can use:

threads.emplace_back(

Instead of:

threads.push_back(

What the commenter also referred to is that the function is creating more than one thread and passing to them a reference to the same boolean return variable.

It will crash if you don't use atomic_bool and even if you do, they will all report back to the same memory location, making the function miss the notification if one of them returns false.

7 Comments

that's why i write this as first argument. And i sent the function address to std::thread. And I know it can works with non-static function member
I have an other part of my code that I compiled some weeks ago and I did call non-static member function with std::thread... I really dont know why it doesnt work... plus it is in the same file...
I tried to rename the overload with an underscore ahead its name, but nothing changed... I start to think to reboot lol
I'm only working within one class. Theres no other class. But still strange... I'll reboot (can try it ^^)
push_back should handle rvalue too. With emplace, you may remove std::thread constructor call.
|
-1

I would get back to reading. Just use another language - Python!

Or use emplace_back

Vladimir Mikov 4044087744

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.