今天apue 的线程章节学习线程退出时,书上对 pthread_exit()和pthread_join 函数的说明不是很详细,看了一个例子还是不理解,说的太过官方
那么先介绍一下
pthread_join(pthread_join thread, void **rval_ptr)
thread = 线程ID , rval_ptr = pthread_exit()的参数,也就是说pthread_exit() 函数的参数是这个线程的返回值,当线程结束后join 函数可以获取他的返回值
pthread_exit(void *rval_ptr)
线程里边不能使用exit _exit _Exit 函数退出,这样会导致进程退出,所以一般使用pthread_exit 函数或者直接return的方式进行退出 那么这个函数的参数是干啥的
rval_ptr 就相当于返回值 join 可以接受到
使用例子书上都谈一下书上说的一个陷阱,需要注意的地方
#include "apue.h"
#include <pthread.h>
struct foo {
int a, b, c, d;
};
void printfoo(const char *s , const struct foo *fp){
printf("%s, ",s);
printf(" struct at 0x%lx\n", (unsigned long)fp);
printf(" foo.a = %d\n", fp->a);
printf("