[英]Semaphore fail to block thread in Win32 console app
我遇到了一個很奇怪的問題:盡管信號量已經為0,但windows.h中的WaitForSingleObject()無法阻止線程。
這是我的代碼:
int main(int argc, char** argv)
{
MsgQueue mainQueue;
//WaitForSingleObject(mainQueue.procSema,(DWORD)(INFINITE));
mainQueue.Enqueue_Back(_T("hello world!"));
mainQueue.Enqueue_Back(_T("This is the second test line"));
mainQueue.Enqueue_Back(_T("Here's the third line"));
TCHAR buffer[256];
int ct = 0;
while(true)
{
cout<<"Start wait ... "<<ct++<<endl;
WaitForSingleObject(mainQueue.procSema,(DWORD)(INFINITE));
memset(buffer,0,256*sizeof(TCHAR));
mainQueue.Dequeue_Front(buffer,255);
cout<<buffer<<endl;
}
return 0;
}
我發現信號量-mainQueue.procSema無法阻止循環,即使它已經等到0。
信號量的聲明和初始化如下:
procSema = CreateSemaphore(NULL,(LONG)(0),(LONG)(INFINITE),NULL);
在每個入隊功能中,都會用一個信號
ReleaseSemaphore(procSema,(LONG)(1),NULL);
我嘗試在我的第一個入隊之前添加多條等待線,但仍然無法正常工作...
請幫幫我 ..
環境:
VC2012 Ult,Windows7 Ult,從一個空項目開始
檢查CreateSemaphore
返回有效的句柄, CreateSemaphore上有以下注釋:
將信號量的最大計數值設置為INFINITE會使相應的句柄無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.