第一個(gè)函數(shù):BOOL CMutexDlg::OnInitDialog(){GloMutex = ::CreateMutex(NULL, FALSE, "Mutex1"); //創(chuàng)建互斥信號量ReleaseMutex(GloMutex);CWinThread *pTSocket = AfxBeginThread(SktThreadFromSer, this, 。。。。)SetTimer(1,2000,NULL);SetTimer(2,5000,NULL);}第二個(gè)相關(guān)函數(shù):void CMutexDlg::OnTimer(UINT nIDEvent) {if(nIDEvent == 1){int i = WaitForSingleObject(GloMutex, 0);SetDlgItemInt(IDC_STATIC,i);}if(nIDEvent == 2)ReleaseMutex(GloMutex);}第三個(gè)相關(guān)函數(shù):UINT SktThreadFromSer(LPVOID pParm){while(1){WaitForSingleObject(GloMutex, INFINITE);AfxMessageBox("fdjasi");ReleaseMutex(GloMutex);Sleep(5500);}}說明:定時(shí)器1定時(shí)2秒,定時(shí)器2定時(shí)5秒,線程睡眠5.5秒,開始線程彈出消息框,然后釋放互斥量,之后睡眠5.5秒,2秒后定時(shí)器1獲取信號,4秒時(shí)還是獲取信號,但此時(shí)返回結(jié)果應(yīng)該是258啊,(0x102),但事實(shí)還是0,這是錯(cuò)誤一,當(dāng)5秒時(shí)釋放信號信號,當(dāng)5.5秒的時(shí)候線程醒來,應(yīng)該會得到信號彈出消息框啊,事實(shí)上沒有,這是錯(cuò)誤二,請教原因!
請教下面函數(shù)出現(xiàn)的錯(cuò)誤問題,是怎么一回事???
Helenr
2023-03-02 22:18:14