Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use pthread_exit() and when to use pthread_join() in Linux?

Tags:

c

linux

pthreads

I am new to pthreads, and I am trying to understand it. I saw some examples like the following.

I could see that the main() is blocked by the API pthread_exit(), and I have seen examples where the main function is blocked by the API pthread_join(). I am not able to understand when to use what?

I am referring to the following site - https://computing.llnl.gov/tutorials/pthreads/. I am not able to get the concept of when to use pthread_join() and when to use pthread_exit().

Can somebody please explain? Also, a good tutorial link for pthreads will be appreciated.

#include <pthread.h> #include <stdio.h> #define NUM_THREADS     5  void *PrintHello(void *threadid) {    long tid;    tid = (long)threadid;    printf("Hello World! It's me, thread #%ld!\n", tid);    pthread_exit(NULL); }  int main (int argc, char *argv[]) {    pthread_t threads[NUM_THREADS];    int rc;    long t;    for(t=0; t<NUM_THREADS; t++){       printf("In main: creating thread %ld\n", t);       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);       if (rc){          printf("ERROR; return code from pthread_create() is %d\n", rc);          exit(-1);       }    }     /* Last thing that main() should do */    pthread_exit(NULL); 

Realized one more thing i.e.

pthread_cancel(thread); pthread_join(thread, NULL); 

Sometimes, you want to cancel the thread while it is executing. You could do this using pthread_cancel(thread);. However, remember that you need to enable pthread cancel support. Also, a clean up code upon cancellation.

thread_cleanup_push(my_thread_cleanup_handler, resources); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);  static void my_thread_cleanup_handler(void *arg) {   // free   // close, fclose } 
like image 337
dexterous Avatar asked Dec 29 '13 10:12

dexterous


People also ask

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

The pthread_exit() API After a call to that function a complicating clean up mechanism is started. When it completes the thread is terminated. The pthread_exit() API is also called implicitly when a call to the return() routine occurs in a thread created by pthread_create().

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 purpose of using pthread_join?

The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate. After the thread terminates, the application may then choose to clean up resources that were used by the thread.

What happens when you call pthread_exit () in the main thread?

When pthread_exit is used, the main thread will stop executing and will remain in zombie(defunct) status until all other threads exit. If you are using pthread_exit in main thread, cannot get return status of other threads and cannot do clean-up for other threads (could be done using pthread_join(3)).


2 Answers

As explained in the openpub documentations,

pthread_exit() will exit the thread that calls it.

In your case since the main calls it, main thread will terminate whereas your spawned threads will continue to execute. This is mostly used in cases where the main thread is only required to spawn threads and leave the threads to do their job

pthread_join will suspend execution of the thread that has called it unless the target thread terminates

This is useful in cases when you want to wait for thread/s to terminate before further processing in main thread.

like image 76
Suvarna Pattayil Avatar answered Sep 18 '22 16:09

Suvarna Pattayil


pthread_exit terminates the calling thread while pthread_join suspends execution of calling thread until target threads completes execution.

They are pretty much well explained in detail in the open group documentation:

  • pthread_exit
  • pthread_join
like image 41
Alok Save Avatar answered Sep 20 '22 16:09

Alok Save