[問題] 發現侯捷老師-多執行緒書中範例的一個問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC6
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
書中範例-TASKQUEM
問題(Question):
console display多出一行
餵入的資料(Input):
6 threads
預期的正確結果(Expected Output):
Launched thread #1 (slot 0)
Launched thread #2 (slot 1)
Launched thread #3 (slot 2)
Slot 0 idle
Slot 0 terminated
Launched thread #4 (slot 0)
Slot 1 idle
Slot 1 terminated
Launched thread #5 (slot 1)
Slot 2 idle
Slot 2 terminated
Launched thread #6 (slot 2)
Slot 0 idle
Slot 1 idle
Slot 2 idle
All slots terminated
錯誤結果(Wrong Output):
Launched thread #1 (slot 0)
Launched thread #2 (slot 1)
Launched thread #3 (slot 2)
Slot 0 idle
Slot 0 terminated
Launched thread #4 (slot 0) ===> 應該只有一個
Launched thread #4 (slot 0) ===> 應該只有一個
Slot 1 idle
Slot 1 terminated
Launched thread #5 (slot 1)
Slot 2 idle
Slot 2 terminated
Launched thread #6 (slot 2)
Slot 0 idle
Slot 1 idle
Slot 2 idle
All slots terminated
程式碼(Code):(請善用置底文網頁, 記得排版)
...
MTVERIFY( hThrds[slot] = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)slot,
0,
&threadId ) );
printf("Launched thread #%d (slot %d)\n", i, slot);
...
範例程式碼可以從原作者的網站上下載下來
http://www.informit.com/store/multithreading-applications-in-win32-the-complete-guide-9780201442342
補充說明(Supplement):
剛compile好,第一次執行,出現此現像的機率超高,
之後大概要執行幾百次才可能會出現一次這個現像.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.222.111.139
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章