我正在處理單生產者單消費者問題。這裏的生產者線程將寫入列表,消費者線程將其從列表中移除。 我有對話框,其中我維護兩個清單1.消費者列表框2.producer列表框,這將列出這兩個線程發佈的消息。我在這裏得到奇怪的問題。這裏的消息被混淆了彼此。消費者線程也從消費者線程消息線程獲取生產者消息,反之亦然。消費者生產者線程問題
我傳遞的ThreadInfo從主線程
任何機構可以建議我出了什麼問題我這兒過得從主線程傳遞正確的線程的名字,但是當涉及到生產者或消費者線程有時價值得到改變。 我有兩個螺紋下方
typedef struct THREADINFO{
CEventQueue* pEventQueue;
HWND hWndHandle;
char* pThreadName;
}THREADINFO
DWORD WINAPI ProducerThrdFunc (LPVOID n)
{
THREADINFO* stThreadInfoProd = (THREADINFO*)n;
char* pMsg1 = new char[100];
while(1)
{
strcpy(pMsg1,stThreadInfoProd->pThreadName);
strcat(pMsg1," Thread No:");
strcat(pMsg1,"Adding Msg");
PostMessage(stThreadInfoProd->hWndHandle,UWM_ONUPDATEPRODUCERLIST,(WPARAM)pMsg1,0);
stThreadInfoProd->pEventQueue->AddTail(pMsg1);
memset(pMsg1,0,100);
strcpy(pMsg1,stThreadInfoProd->pThreadName);
strcat(pMsg1,"Thread No:");
strcat(pMsg1,"Added Msg");
char*p = "Producer";
PostMessage(stThreadInfoProd->hWndHandle,UWM_ONUPDATEPRODUCERLIST,(WPARAM)pMsg1,0);
Sleep(3000);
}
return 0;
}
DWORD WINAPI ConsumerThrdFunc (LPVOID n)
{
THREADINFO* stThreadInfoCons = (THREADINFO*)n;
char* pMsg = new char[100];
memset(pMsg,0,100);
while(1)
{
strcpy(pMsg,stThreadInfoCons->pThreadName);
strcat(pMsg," Thread No:");
strcat(pMsg,"Removing Msg");
PostMessage(stThreadInfoCons->hWndHandle,UWM_ONUPADTECONSUMERLIST,(WPARAM)pMsg,0);
memset(pMsg,0,100);
char *pMsg = (char*)stThreadInfoCons->pEventQueue->RemoveHead();
strcpy(pMsg,stThreadInfoCons->pThreadName);
strcat(pMsg,"Thread No:");
strcat(pMsg,"Removed Msg");
PostMessage(stThreadInfoCons->hWndHandle,UWM_ONUPADTECONSUMERLIST,(WPARAM)pMsg,0);
Sleep(3000);
}
return 0;
}
顯示代碼分別啓動線程,並通過一個ThreadInfo的說法,應該有問題。 – Fox32 2011-03-03 07:57:54
你的意思是「有時候值得改變」? – Fox32 2011-03-03 09:48:48