线程函数执行到中途需等待信号执行另一操作,这时没有等到信号,被(flag)return结束了,到不了delete,求各位给个方案。
unsigned __stdcall CPrintThreadDlg::ThreadProcP(void *lparam)
{
CPrintThreadDlg*pthis = (CPrintThreadDlg*)lparam;
while (pthis->m_bFlagQuit)
{
int *p = new int;
WaitForSingleObject(pthis->m_hEvent, INFINITE);
while (m_bFlagQuit&&pthis->m_Goods <= 1000)
{
//等到信号生产产品
//InterlockedIncrement((LPLONG)pthis->m_Goods);
//WaitForSingleObject(pthis->m_hMutex,INFINITE);
pthis->m_Goods++;
//ReleaseMutex(pthis->m_hMutex);
//将产品加入到队列
EnterCriticalSection(&pthis->m_cs);
//pthis->m_Goods++;
pthis->m_qu.push(pthis->m_Goods);
LeaveCriticalSection(&pthis->m_cs);
//给消费者释放信号
ReleaseSemaphore(pthis->m_hSemaphore, 1, NULL);
}
delete p;
p = NULL;
}
return 0;
}