Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Detach / Joinable Methods

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?

like image 928
Joy Hyuk Lee Avatar asked Sep 07 '14 14:09

Joy Hyuk Lee


People also ask

What are joinable and detachable threads?

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.

What is the difference between a joinable thread and a detached thread?

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

How do you detach threads?

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.

What are joinable threads?

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.


1 Answers

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.

like image 159
usr Avatar answered Oct 20 '22 23:10

usr