[問題] thread中斷點調式後的結果與執行不同
//使用PluseEvent()函數
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <windows.h>
HANDLE g_hThreadEvent;
//快線程
unsigned int __stdcall FastThreadFun(void *pPM)
{
Sleep(10); //用這個來保證各線程調用等待函數的次序有一定的隨機性
printf("%s 啟動\n", (PSTR)pPM);
WaitForSingleObject(g_hThreadEvent, INFINITE);
printf("%s 等到事件被觸發 順利結束\n", (PSTR)pPM);
return 0;
}
//慢線程
unsigned int __stdcall SlowThreadFun(void *pPM)
{
Sleep(100);
printf("%s 啟動\n", (PSTR)pPM);
WaitForSingleObject(g_hThreadEvent, INFINITE);
printf("%s 等到事件被觸發 順利結束\n", (PSTR)pPM);
return 0;
}
int main()
{
printf(" 使用PluseEvent()函數\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
BOOL bManualReset = FALSE;
//創建事件 第二個參數手動置位TRUE,自動置位FALSE
g_hThreadEvent = CreateEvent(NULL, bManualReset, FALSE, NULL);
if (bManualReset == TRUE)
printf("當前使用手動置位事件\n");
else
printf("當前使用自動置位事件\n");
char szFastThreadName[5][30] = {"快線程1000", "快線程1001", "快線程1002", "快線程1003", "快線程1004"};
char szSlowThreadName[2][30] = {"慢線程196", "慢線程197"};
int i;
for (i = 0; i < 5; i++)
_beginthreadex(NULL, 0, FastThreadFun, szFastThreadName[i], 0, NULL);
for (i = 0; i < 2; i++)
_beginthreadex(NULL, 0, SlowThreadFun, szSlowThreadName[i], 0, NULL);
Sleep(50); //保證快線程已經全部啟動
printf("現在主線程觸發一個事件脈衝 - PulseEvent()\n");
PulseEvent(g_hThreadEvent);//調用PulseEvent()就相當於同時調用下面二句
//SetEvent(g_hThreadEvent);
//ResetEvent(g_hThreadEvent);
Sleep(3000);
printf("時間到,主線程結束運行\n");
CloseHandle(g_hThreadEvent);
return 0;
}
如上,我在快線程和慢線程分別加入了中斷點去看結果,發現插入中斷點後
最後的執行結果與不插入中斷點的執行結果是有差的,試了幾次以後發現都是這樣
是什麼原因導致這樣的結果呢? 理論上中斷了所有線程會一起停止
結果應該會一樣不是嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.105
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1411869774.A.A00.html
→
09/28 10:10, , 1F
09/28 10:10, 1F
→
09/28 10:11, , 2F
09/28 10:11, 2F
→
09/28 10:12, , 3F
09/28 10:12, 3F
→
09/28 11:24, , 4F
09/28 11:24, 4F
→
09/28 14:49, , 5F
09/28 14:49, 5F
→
09/28 15:26, , 6F
09/28 15:26, 6F
→
09/29 09:39, , 7F
09/29 09:39, 7F
→
09/29 09:39, , 8F
09/29 09:39, 8F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章