作者abcd991276 (QQ)
看板C_and_CPP
標題[問題] 為什麼會印兩次child
時間Thu Feb 17 21:10:15 2022
https://i.imgur.com/nIqiVdt.jpg
我把X設成1
他會印出
parent: begin
parent: end
child
child
X設成0的時候
會印出
parent: begin
child
Parent: end
很好奇為什麼parent先結束
child會印兩次
----
Sent from
BePTT
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.174.95.189 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1645103417.A.BB2.html
1F:推 lc85301: 我沒辦法重現,只會印一次 child 02/17 22:59
2F:推 kingofsdtw: 貼圖我懶的看程式 02/22 02:23
3F:→ kingofsdtw: 至少弄個複製貼上吧 02/22 02:23
4F:→ TianBonBon: 這什麼環境和編譯器? 02/22 13:18
5F:→ abcd991276: 在win 10的Ubuntu子系統 我用Gcc 編譯的 02/22 19:19
6F:推 Domos: 改成children就正確了 02/22 20:58
7F:→ lc85301: 樓上笑死XDD 02/22 23:51
8F:→ sarafciel: 應該thread被綠了才會多一個child出來 請左轉男女版(X 02/23 09:36
9F:→ TianBonBon: 傳給pthread_create()的c應該是要&c吧? 沒出SEGV反而 02/23 12:42
10F:→ TianBonBon: 恐怖 02/23 12:42
11F:→ Dracarys: 1. 使用未初始化變數 c -> undefined behavior,大概少 02/23 14:17
12F:→ Dracarys: 打& 02/23 14:17
13F:→ Dracarys: 2. ld.lld: error: undefined symbol: Pthread_create 02/23 14:17
14F:→ Dracarys: ,libpthread.so只有pthread_create。 02/23 14:17
15F:→ Dracarys: 3. X設1,沒join大概率child不會印 02/23 14:17
16F:噓 Dracarys: Warning開多一點,輔以ubsan、tsan怎麼可能沒發現 02/23 14:22
18F:推 n1canhelpu: 0.0 &呢? 03/02 07:50