我程式码中把pthread_join 注解test函数thread无法印完10次
但是我网络看到解释 pthread_join 让main thread 等待test函数的做完才能做资源释放
为什么 main thread 结束 也会影响 test函数的thread 也跟着结束?
而不是像 process 父行程先死掉变成孤儿的概念 由 init 行程去回收?
谢谢
#include <pthread.h>
void test(void)
{
int i;
for (i = 0; i < 10; i++) {
printf("This is a thread n\n");
}
}
int main(void)
{
pthread_t id;
int i, ret;
pthread_create(&id, NULL, (void*) test, NULL);
for (i = 0; i < 2; i++) {
printf("This is the process n");
}
// pthread_join(id, NULL);
return (0);
}
https://gist.github.com/shihyu/10f7021b4a0f277b1cf9d045a8d41e86
我不懂是 fork 的父行程先结束 , 子行程可以印完才结束,
但是子thread会被父thread 影响到提早结束