When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.
修正一下 for (int i = nThread; i < nIndex - 1; i ++) { hThreads[i] = hThreads[i + 1]; nInfos[i] = nInfos[i + 1]; }
详看MSDN上的说明 返回值的说明已经很明确了 Return Values If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following. Value Meaning WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount – 1) If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled. If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signalled during the call, this is the array index of the signalled object with the smallest index value of all the signalled objects.
WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 + nCount – 1) If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object. If bWaitAll is FALSE, the return value minus WAIT_ABANDONED_0 indicates the lpHandles array index of an abandoned mutex object that satisfied the wait.
WAIT_TIMEOUT The time-out interval elapsed and the conditions specified by the bWaitAll parameter are not satisfied. If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. 所以要等全部结束 dwRes = ::WaitForMultipleObjects(MAX_THREAD_NUM, ghThreads, TRUE, INFINITE); 或者是每一个单独等 WaitForSingleObject
{
for(int j=0;j<10;j++)
{
theport[j].rmt_host=rmt_host;
theport[j].p=port[i*10+j];
theport[j].n=j;
Thread[j]=AfxBeginThread(pScan,(LPVOID)&theport[j]);
hThread[j]=Thread[j]->m_hThread;
Sleep(1);
}
WaitForMultipleObjects(10,hThread,TRUE,120000);
}注:线程退出后,即线程对象计数值变为0后,线程才会变为受信状态。
#include <stdio.h>
#include <conio.h>
#include <tchar.h>#define MAX_THREAD_NUM 3DWORD WINAPI ThreadProc(LPVOID lpParam)
{
printf("线程%d运行中...\n", (int)lpParam);
Sleep(1000 * 2);
return 0;
}int _tmain(int argc, TCHAR **argv)
{
HANDLE hThreads[MAX_THREAD_NUM];
int nInfos[MAX_THREAD_NUM];
int nIndex;
int nThread = 0;
DWORD dwRes = NULL;
DWORD dwThreadID = NULL; for(nIndex = 0; nIndex < MAX_THREAD_NUM; nIndex ++)
{
hThreads[nIndex] = CreateThread(NULL, 0, ThreadProc, (LPVOID)nIndex, 0, &dwThreadID);
nInfos[nIndex] = nIndex;
} for (nIndex = MAX_THREAD_NUM; nIndex > 0; nIndex --)
{
//等待哪个线程结束
dwRes = ::WaitForMultipleObjects(nIndex, hThreads, FALSE, INFINITE);
nThread = dwRes - WAIT_OBJECT_0;
printf("%d\n", nInfos[nThread]); CloseHandle(hThreads[nThread]);
for (int i = nThread; i < MAX_THREAD_NUM - 1; i ++)
{
hThreads[i] = hThreads[i + 1];
nInfos[i] = nInfos[i + 1];
}
} getch();
return 0;
}只能认为,线程退出后,其句柄会一直处于受信状态
再加上WaitForMultipleObjects每次都是从头开始检测句柄的
这里的方法是将退出的线程句柄从数组中移除
for (int i = nThread; i < nIndex - 1; i ++)
{
hThreads[i] = hThreads[i + 1];
nInfos[i] = nInfos[i + 1];
}
Return Values
If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following. Value Meaning
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount – 1) If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled.
If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signalled during the call, this is the array index of the signalled object with the smallest index value of all the signalled objects.
WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 + nCount – 1) If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object.
If bWaitAll is FALSE, the return value minus WAIT_ABANDONED_0 indicates the lpHandles array index of an abandoned mutex object that satisfied the wait.
WAIT_TIMEOUT The time-out interval elapsed and the conditions specified by the bWaitAll parameter are not satisfied.
If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. 所以要等全部结束
dwRes = ::WaitForMultipleObjects(MAX_THREAD_NUM, ghThreads, TRUE, INFINITE);
或者是每一个单独等 WaitForSingleObject
有点看不明白。就是说,关闭了的HANDLE,需要从数据中去掉?
用TRUE是好判断。但用FALSE等待任何一个激发不好做...