[問題] pthread_join 作用
我程式碼中把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 影響到提早結束
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.182.113.117
※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1543068296.A.7DE.html
→
11/24 22:35,
6年前
, 1F
11/24 22:35, 1F
※ 編輯: shihyu (175.182.113.117), 11/24/2018 23:39:50
推
11/25 14:51,
6年前
, 2F
11/25 14:51, 2F
→
11/25 14:51,
6年前
, 3F
11/25 14:51, 3F
→
11/25 15:02,
6年前
, 4F
11/25 15:02, 4F
→
11/25 15:02,
6年前
, 5F
11/25 15:02, 5F
→
11/25 15:03,
6年前
, 6F
11/25 15:03, 6F
→
11/25 15:03,
6年前
, 7F
11/25 15:03, 7F
推
11/25 17:56,
6年前
, 8F
11/25 17:56, 8F
→
11/25 17:58,
6年前
, 9F
11/25 17:58, 9F
→
11/25 17:59,
6年前
, 10F
11/25 17:59, 10F
→
11/26 19:26,
6年前
, 11F
11/26 19:26, 11F
推
11/26 19:28,
6年前
, 12F
11/26 19:28, 12F
推
11/27 21:58,
6年前
, 13F
11/27 21:58, 13F
※ 編輯: shihyu (106.104.69.161), 11/27/2018 21:58:56
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章