1 pthread_join函数
int pthread_join(pthread_t thread, void **retval);
2 pthread_join函数使用案例
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct {
char ch;
int var;
char str[64];
}th_t;
void *thrd_func(void *arg){
th_t *retvar = (th_t *)arg;
retvar->ch = 'm';
retvar->var = 200;
strcpy(retvar->str, "my thread");
pthread_exit((void *)retvar);
}
int main(void){
pthread_t tid;
int ret;
th_t *retval = NULL;
retval = malloc(sizeof(th_t))
ret = pthread_create(&tid, NULL, thrd_func, (void *)retval);
if (ret != 0) {
fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
exit(1);
}
pthread_join(tid, (void **)&retval);
printf("ch = %c, var = %d, str = %s\n", retval->ch, retval->var, retval->str);
if(retval != NULL){
free(retval);
retval = NULL;
}
pthread_exit((void *)1);
}