If I don't care about the return status of my thread, would I need to have a pthread_exit?
I'm wondering if there might be some subtle resource problems associated with not calling pthread_exit in my datached pthreads.
Thanks.
You are not required to call pthread_exit . The thread function can simply return when it's finished.
Also, in main() , return will implicitly call exit() , and thus terminate the program, whereas pthread_exit() will merely terminate the thread, and the program will remain running until all threads have terminated or some thread calls exit() , abort() or another function that terminates the program.
Return Value pthread_exit() does not return.
2. Passing Multiple Arguments to Threads. When passing multiple arguments to a child thread, the standard approach is to group the arguments within a struct declaration, as shown in Code Listing 6.9. The address of the struct instance gets passed as the arg to pthread_create() .
The purpose pthread_exit()
is to return the exit code if any other threads that joins.
From the manual:
Performing a return from the start function of any thread other than the main
thread results in an implicit call to pthread_exit(), using the function's
return value as the thread's exit status.
So, it makes no difference if you don't use it.
You don't have to call pthread_exit()
. Returning from the thread function would work equally well, and will not leak any resources (of course, you still have to ensure that your code doesn't have any leaks).
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