简体   繁体   中英

why using pthread_exit?

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!

  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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM