Where are the C# Thread Detach
/ Joinable
functions?
In C++ 11 these functions are available:
thread::joinable()
thread::detach()
But they can't be found in .NET - where are they?
Joinable or Not? When a thread is created, one of its attributes defines whether it is joinable or detached. Only threads that are created as joinable can be joined. If a thread is created as detached, it can never be joined. The final draft of the POSIX standard specifies that threads should be created as joinable.
Performance-wise, there's no difference between joinable threads vs detached threads. The only difference is that with detached threads, its resources (such as thread stack and any associated heap memory, and so on - exactly what constitutes those "resources" are implementation-specific).
std::thread::detach. Separates the thread of execution from the thread object, allowing execution to continue independently. Any allocated resources will be freed once the thread exits.
Thread joinable() function in C++A thread object is said to be joinable if it identifies/represent an active thread of execution. A thread is not joinable if: It was default-constructed. If either of its member join or detach has been called. It has been moved elsewhere.
What would detach
do in your mind in .NET? .NET has no automatic storage duration. This concept does not apply.
joinable
doesn't really apply either because you can Join
any thread, even Thread.Current
. If you insist on C++ semantics:
Thread someThread = ...;
bool isJoinable = Thread.Current != someThread;
Thread
objects are subject to Garbage Collection but a thread runs to completion regardless of whether the Thread
object is still reachable. Don't worry about this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With