std::thread finishes before I can detach it [duplicate]
Asked Answered
R

2

4

If I create an std::thread that terminates before I am able to call detatch() on it, what is the expected behavior? Should an exception be thrown due to the fact that joinable is already false?

If this is so, then is there a way to create a thread which is initialized into a detached state so that I can avoid this error?

Example:

void func()
{
    // do something very trivial so that it finishes fast
    int a = 1;
}

void main()
{
    thread trivial(func);
    trivial.detach();
}

I realize that it isn't really practical to spawn a thread to do trivial work in practice, however I did manage to run into this behavior, and I was curious to know if there is a way around it...

Rager answered 10/12, 2014 at 5:50 Comment(4)
The result of joinable() does not depend on whether the thread has finished execution. In your scenario, joinable() does not in fact become false until you call detach(). There is no problem here in need of a workaround.Damselfish
A thread that has finished executing code, but has not yet been joined is still considered an active thread of execution and is therefore joinable.Snowdrop
Good points. I am getting this behavior in Visual Studio 2012. Perhaps some kind of optimization error?Rager
Alex Farber quote comes from en.cppreference.com/w/cpp/thread/thread/joinableBurin
G
1

Join and detach have the same requirement, they have to be joinable. So I think doing the joinable check should suffice.

If(trivial.joinable())
    trivial.detach();

The documentation states:

A thread that has finished executing code, but has not yet been joined is still considered an active thread of execution and is therefore joinable.

So my guess would be that the detached thread will cease to exist right away. But it definitily can be created.

Note: It's good practice to always call joinable() before joining or detaching a thread.

Guzel answered 15/5, 2015 at 8:58 Comment(4)
This of course contains a race condition as it could become unjoinable after the check but before the detach.Thermocouple
@Thermocouple That would be true if the tread would become unjoinable in the meantime. How do you envision this happening?Guzel
If a thread is indeed finished and you want to delete the thread. Is detach() faster vs join()? I guess so, right?Dentation
@Thermocouple if the thread became unjoinable between the joinable() and detach() calls then that's the least of your worries as the developer. Something else is causing undefined behavior, and the race condition is just the manifestation of that problem. It means that other places of the code are moving the thread object during the call, which is a big nono in the first place.Actin
F
1

The thread object will be destructed when its scope will be done. To make sure that object finished its job you need either call join or detach.

Fell answered 12/8, 2015 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.