Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need pthread_exit if I don't care of return value

Tags:

c++

c

pthreads

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.

like image 539
monkeyking Avatar asked Dec 21 '12 12:12

monkeyking


People also ask

Is pthread_exit necessary?

You are not required to call pthread_exit . The thread function can simply return when it's finished.

What is the difference between return and pthread_exit in a function associated to a thread?

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.

What does pthread_exit return?

Return Value pthread_exit() does not return.

How can we pass multiple values to a thread?

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


2 Answers

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.

like image 85
P.P Avatar answered Sep 28 '22 06:09

P.P


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

like image 34
NPE Avatar answered Sep 28 '22 06:09

NPE