I am complete new to pthreads and I wonder what the exact differences are.
pthread_exit exits a thread. and thus pthread_join will return; However what does detach do that is different from pthread_join?
for instance I create a thread and lets say the thread is finished and I want to completely terminate the thread so I can recreate it later. What is better to use. pthread_join or pthread_detach?
so the order of execution is
pthread_exit();
pthread_join(); or pthread_detach();
?
pthread_exit is called from the thread itself to terminate its execution (and return a result) early. pthread_join is called from another thread (usually the thread that created it) to wait for a thread to terminate and obtain its return value.
The pthread_detach() function is used to indicate to your application that storage for the thread tid can be reclaimed when the thread terminates. Threads should be detached when they are no longer needed. If tid has not terminated, pthread_detach() does not cause the thread to terminate.
On return from a successful pthread_join() call with a non-NULL value_ptr argument, the value passed to pthread_exit() by the terminating thread shall be made available in the location referenced by value_ptr. When a pthread_join() returns successfully, the target thread has been terminated.
Assuming the main thread is joinable, there's no problem if main calls pthread_exit before start calls pthread_join . The semantics of a joinable thread are like those of a pid for a child process.
Allows the calling thread to wait for the ending of the target thread. pthread_t is the data type used to uniquely identify a thread. It is returned by pthread_create() and used by the application in function calls that require a thread identifier.
The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.
pthread_exit
is called from the thread itself to terminate its execution (and return a result) early.
pthread_join
is called from another thread (usually the thread that created it) to wait for a thread to terminate and obtain its return value. It can be called before or after the thread you're waiting for calls pthread_exit
. If before, it will wait for the exit to occur. If after, it simply obtains the return value and releases the pthread_t
resources.
pthread_detach
can be called from either the thread itself or another thread, and indicates that you don't want the thread's return value or the ability to wait for it to finish. This is useful because otherwise, until you call pthread_join
, the pthread_t
value remains valid and consumes resources - at the very least, resources to store the return value and tying up one possible value of pthread_t
. If you're using pthread_detach
, normally you call it from either the new thread or the creating thread as soon as the new thread is created (right after pthread_create
).
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