void WaitForThreadExit(void)
{
DWORD dwRet;
MSG msg;
int wait_count=4;
int nExitThreadCount=0;
while(1)
{
dwRet = MsgWaitForMultipleObjects(wait_count, hArray, FALSE, INFINITE, QS_ALLINPUT);
if (dwRet == WAIT_OBJECT_0 + wait_count)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message==WM_QUIT||msg.message==WM_CLOSE)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}else if (dwRet >= WAIT_OBJECT_0 && dwRet < WAIT_OBJECT_0+ wait_count)
{
nExitThreadCount++;
if (nExitThreadCount < 4)
{
TRACE("一个线程退出了\n");
int nIndex=dwRet-WAIT_OBJECT_0;
hArray[nIndex]=hArray[wait_count-1];
hArray[wait_count-1]=NULL;
wait_count--;
}else
{
TRACE("4个线程都退出了\n");
break;
}
}else
{
DWORD dErrCode=GetLastError();
break;
}
}
}

本文介绍了一个用于等待多个线程退出的函数实现。该函数通过MsgWaitForMultipleObjects等待多个线程对象,当线程退出时调整等待数组并减少计数,直至所有线程退出。
2563

被折叠的 条评论
为什么被折叠?



