2

I'm trying to figure out the usage of pthread_exit using this example code:

void* PrintVar(void* arg)
 { 
   int * a = (int *) arg; // we can access memory of a!!!
    printf( "%d\n", *a); 
 } 

int main(int argc, char*argv[]) 
 { 
   int a, rc;
    a = 10; 
   pthread_t thr; 
   pthread_create( &thr, NULL, PrintVar, &a ); 

  //why do I need it here?//
  pthread_exit(&rc); /* process continues until last  
                                threads termintates */

there are two things I'm not quite sure about :

  1. when we are using pthread_create - I'm passing 'a' parameter's address, but is this paramter being "saved" under "arg" of the PrintVar function? for example if I was using : PrintVar(void *blabla) , and wanted to pass 2 parameters from main function : int a = 10, int b= 20 .. how can I do that?

  2. Why the pthread_exit needed? it means - wait for proccess to end - but what scenario can I get if I won't use that line?

thanks alot!

4
  • Referring the 2nd question: Did you tried out what will happen? Hints: Add a sleep(1); to the beginning of PrintVar() and comment out the call to pthread_exit().
    – alk
    May 6, 2013 at 13:21
  • 1
    these are basic questions for multi threading, also quite intelligent question. computing.llnl.gov/tutorials/pthreads perhaps a good book will help more
    – abasu
    May 6, 2013 at 13:24
  • @alk I used it without sleep and it worked the same as it would without any threading..thats why I'm confused:( I'll try what u suggested! thank you May 6, 2013 at 13:28
  • (stackoverflow.com/questions/6042970/pthread-detach-question) Somewhat similar question
    – puffadder
    May 7, 2013 at 9:21

2 Answers 2

4
  1. when we are using pthread_create - I'm passing 'a' parameter's address, but is this paramter being "saved" under "arg" of the PrintVar function?

The "original" a (the one defined in main) is not being copied, you are only passing around a pointer to it.

for example if I was using : PrintVar(void *blabla) , and wanted to pass 2 parameters from main function : int a = 10, int b= 20 .. how can I do that?

Put those two values in a struct and pass a pointer to such struct as argument to pthread_create (PrintVar, thus, will receive such a pointer and will be able to retrieve the two values).

and my second question is why the pthread_exit needed? it means - wait for proccess to end - but what scenario can I get if I won't use that line?

pthread_exit terminates the current thread without terminating the process if other threads are still running; returning from main, instead, is equivalent to calling exit which, as far as the standard is concerned, should "terminate the program" (thus implicitly killing all the threads).

Now, being the C standard thread-agnostic (until C11) and support for threading in the various Unixes a relatively recent addition, depending from libc/kernel/whatever version exit may or may not kill just the current thread or all the threads.

Still, in current versions of libc, exit (and thus return from main) should terminate the process (and thus all its threads), actually using the syscall exit_group on Linux.

Notice that a similar discussion applies for the Windows CRT.

7
  • so if im using exit() it will terminate all threads, but if i'm using pthread_exit it will wait for which of the threads to finish?the one that is running? meaning the OS can't stop the thread "in the middle"? May 6, 2013 at 13:47
  • @user1386966: if you use pthread_exit it will kill only the current thread, leaving the others running; the process will terminate automatically when the last thread terminates. Using exit you will brutally terminate all the threads. Notice that, for a "normal" thread, returning from its entrypoint is equivalent to calling pthread_exit; the problem is just with the main thread, since returning from main is equivalent to calling exit. May 6, 2013 at 13:49
  • 2
    +1 I'd add that it's rather poor practice to pass the address of automatic storage (stack) variables to thread start routines -- that stack might be gone by the time the new thread wakes up to inspect its arguments.
    – pilcrow
    May 6, 2013 at 14:20
  • 1
    What @pilcrow said, and it's not just poor practice, but extremely dangerous. Pointers passed to thread functions should either be dynamically allocated (and their ownership transferred to the thread), or point to global data. May 6, 2013 at 15:40
  • @pilcrow Incase there was a pthread_join(thr)instead of pthread_exit, then will it be safe to assume the value would be present on the stack when the thread examines it? [Was just going through the code and had this doubt] May 7, 2013 at 11:01
-1

The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit(3) (or equivalently, if the main thread returns).

1
  • Welcome to SO! Check the question and check your answer. It is rather a comment than an answer. Please edit or delete as some OP could downvote it. Dec 1, 2019 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.