我程式碼中把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 影響到提早結束