pthread_join function
Quit waiting thread, the thread gets its role exit status, the corresponding process waitpid () function.
int pthread_join (pthread_t thread, void ** retval); Success: 0; failed: No.
Parameters: Thread: Thread ID ([Note]: not a pointer); retval: storing thread end state.
Contrast Memory:
Process: main return values, exit parameters -> int; wait for the child ended the wait function parameters -> int *
Thread: thread main function return values, pthread_exit -> void *; waiting thread ends pthread_join function parameters -> void **
[Practice]: non-null parameters retval usage. [Pthrd_exit_join.c]
The function is called thread hangs wait until terminated as thread id thread. thread thread terminated in different ways, by terminating the state is different pthread_join obtained, summarized as follows:
- If the thread through the thread return return retval unit pointed in the return value is stored in thread thread function.
- If the thread is called a thread pthread_cancel another thread terminates abnormally out, retval unit is pointing in the store is constant PTHREAD_CANCELED.
- If the thread is a thread calls himself pthread_exit terminated, retval pointed unit is stored in the parameters passed to pthread_exit.
- If you are not interested in the state of thread to terminate the thread, you can pass NULL to retval parameter.
[Practice]: using multiple child threads pthread_join recovery function will cycle created.
/*** thread_join.c ***/ #include<stdio.h> #include<error.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<pthread.h> typedef struct { char ch; int var; char str[64]; }exit_t; void *thrd_func(void *arg) { pthread_exit((void *)1); } int main() { pthread_t tid; int ret; int *retval; printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid()); ret = pthread_create(&tid,NULL,thrd_func,NULL); if(0 != ret) { fprintf(stderr,"pthread_create error : %s \n",strerror(ret)); exit(1); } pthread_join(tid,(void **)&retval); printf("--------------%d\n",(int)retval); pthread_exit((void*)1); }
operation result:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join
In main 1 : thread id = 139721544345344, pid = 12974
--------------1
/*** pthread_join_struct.c ***/ #include<stdio.h> #include<error.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<pthread.h> typedef struct { char ch; int var; char str[64]; }exit_t; void *thrd_func(void *arg) { exit_t *retvar = (exit_t *)malloc(sizeof(exit_t)); retvar->ch = 'm'; retvar->var = 200; strcpy(retvar->str,"my thread\n"); pthread_exit((exit_t *)retvar); } int main() { pthread_t tid; int ret; exit_t *retval; printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid()); ret = pthread_create(&tid,NULL,thrd_func,NULL); if(0 != ret) { fprintf(stderr,"pthread_create error : %s \n",strerror(ret)); exit(1); } pthread_join(tid,(void **)&retval); printf("ch = %c,var = %d,str = %s\n--------------%d\n",retval->ch,retval->var,retval->str); free(retval); pthread_exit((void*)1); }
operation result:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join
In main 1 : thread id = 140316487100160, pid = 13121
ch = m,var = 200,str = my thread
--------------202
/*** thread_join_loop.c ***/ #include<stdio.h> #include<string.h> #include<unistd.h> #include<pthread.h> int var = 100; void *tfn(void *arg) { int i; i = (int)arg; sleep(i); if(1 == i) { var = 333; printf("var = %d\n",var); return (void*)var; } else if( 3 == i) { var = 888; printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var); pthread_exit((void*)var); } else { printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var); pthread_exit((void*)var); } return NULL; } int main() { pthread_t tid[5]; int i; int *ret[5]; for(i = 0; i < 5; i++) { pthread_create(&tid[i],NULL,tfn,(void*)i); } for(i = 0; i < 5; i++) { pthread_join(tid[i],(void**)&ret[i]); printf("-----------%d's ret = %d\n",(int)ret[i]); } printf("I'm main pthread tid = %lu var = %d\n",pthread_self(),var); sleep(i); return 0; }
operation result:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_loop
i'm 1th pthread,pthread id = 139809124009728 ,var = 100
----------- 100's right = -645238160
was = 333
----------- 333's right = -651,408,960
i'm 3th pthread,pthread id = 139809107224320 ,var = 333
----------- 333's right = -659,801,664
i'm 4th pthread,pthread id = 139809098831616 ,var = 888
----------- 888 K = -668194368
i'm 5th pthread,pthread id = 139809090438912 ,var = 888
----------- 888 K = 0
I'm main pthread tid = 139809132349184 var = 888