本帖最后由 OOOqqqOOO 于 2010-01-20 07:17:35 编辑

解决方案 »

  1.   

    WaitForMultipleObjects函数用于等待多个内核对象,前两个参数分别为要等待的内核对象的个数和句柄数组指针。如果将第三个参数bWaitAll的值设为TRUE,等待的内核对象全部变成受信状态以后此函数才返回。否则,bWaitAll为0的话,只要等待的内核对象中有一个变成了受信状态,WaitForMultipleObjects就返回,返回值指明了是哪一个内核对象变成了受信状态。实例一:下面的代码说明了函数返回值的作用:         HANDLE h[2];         h[0] = hThread1;         h[1] = hThread2;         DWORD dw = ::WaitForMultipleObjects(2, h, FALSE, 5000);         switch(dw)         {       case WAIT_FAILED:                            // 调用WaitForMultipleObjects函数失败(句柄无效?)                            break;                   case WAIT_TIMEOUT:                            // 在5秒内没有一个内核对象受信                            break;                   case WAIT_OBJECT_0 + 0:                            // 句柄h[0]对应的内核对象受信                            break;                   case WAIT_OBJECT_0 + 1:                            // 句柄h[1]对应的内核对象受信                            break;         }参数bWaitAll为FALSE的时候,WaitForMultipleObjects函数从索引0开始扫描整个句柄数组,第一个受信的内核对象将终止函数的等待,使函数返回。实例二:for(int i=0;i<6;i++)
    {
       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后,线程才会变为受信状态。
      

  2.   

    #include <windows.h>
    #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每次都是从头开始检测句柄的
    这里的方法是将退出的线程句柄从数组中移除
      

  3.   

    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.
      

  4.   

    修正一下
    for (int i = nThread; i < nIndex - 1; i ++)
    {
         hThreads[i] = hThreads[i + 1];
         nInfos[i] = nInfos[i + 1];
    }
      

  5.   

    详看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
      

  6.   

    如果bWaitAll是FALSE,那么将返回值减去WAIT_OBJECT_0,就表示数据中的哪一个handle被激发。这样的啊?但我为什么总是返回0?而不是0~3之间呢?
      

  7.   

    你要用 Closehandle 关掉线程的句柄
      

  8.   


    有点看不明白。就是说,关闭了的HANDLE,需要从数据中去掉?
      

  9.   


    用TRUE是好判断。但用FALSE等待任何一个激发不好做...