Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pthread_exit, pthread_join and pthread_detach

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

?

like image 657
user3021085 Avatar asked Mar 15 '14 17:03

user3021085


People also ask

What is the difference between pthread_exit and pthread_join?

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.

What is pthread_detach?

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.

What is the use of pthread_join () and pthread_exit () function?

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.

Can you call pthread_join on the main thread?

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.

What does pthread_create and pthread_join do?

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.

What is the purpose of pthread_join?

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.


1 Answers

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

like image 149
R.. GitHub STOP HELPING ICE Avatar answered Oct 31 '22 12:10

R.. GitHub STOP HELPING ICE