首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > windows >

WaitForMultipleOjbect 终究什么意思

2012-12-31 
WaitForMultipleOjbect 到底什么意思?本帖最后由 walfud 于 2012-03-07 10:10:24 编辑#include iostream

WaitForMultipleOjbect 到底什么意思?
本帖最后由 walfud 于 2012-03-07 10:10:24 编辑


#include <iostream>
#include <windows.h>
using namespace std;

#define MAX_SEM_COUNT 10
#define THREADCOUNT 12

HANDLE ghSemaphore;

DWORD WINAPI ThreadProc( LPVOID );

void main()
{
    HANDLE aThread[THREADCOUNT];
    DWORD ThreadID;
    int i;

    // Create a semaphore with initial and max counts of MAX_SEM_COUNT

    ghSemaphore = CreateSemaphore( 
        NULL,           // default security attributes
        MAX_SEM_COUNT,  // initial count
        MAX_SEM_COUNT,  // maximum count
        NULL);          // unnamed semaphore

    if (ghSemaphore == NULL) 
    {
        printf("CreateSemaphore error: %d\n", GetLastError());
        return;
    }

    // Create worker threads

    for( i=0; i < THREADCOUNT; i++ )
    {
        aThread[i] = CreateThread( 
                     NULL,       // default security attributes
                     0,          // default stack size
                     (LPTHREAD_START_ROUTINE) ThreadProc, 
                     NULL,       // no thread function arguments
                     0,          // default creation flags
                     &ThreadID); // receive thread identifier

        if( aThread[i] == NULL )
        {
            printf("CreateThread error: %d\n", GetLastError());
            return;
        }
    }

    // Wait for all threads to terminate
    WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);   // 这个函数到底是什么作用? 什么叫做等待内核对象?



    // Close thread and semaphore handles

    for( i=0; i < THREADCOUNT; i++ )
        CloseHandle(aThread[i]);

    CloseHandle(ghSemaphore);
}

DWORD WINAPI ThreadProc( LPVOID lpParam )
{
    BOOL bContinue=TRUE;

    while(bContinue)
    {}
    return TRUE;
}


[解决办法]
等待多个handle受信状态。像是很多内核的对象的handle(比如线程的, 事件的, 信号量的)都是有受信,为受信状态。  比如说线程的handle,运行时刻等的状态都是未受信,只有该线程结束后,其handle的状态就变成了受信。

当线程还在运行的时候, waitforobject(某线程的handle);程序就会等在这个语句上,等待该handle变成受信状态。当线程结束运行后,该线程的handle成为受信状态, waitforobject(该线程的handle)这条语句,就可以自行完毕。开始执行下面的语句了。

请给我分我,我太穷了。(感觉我说得已经很清楚了,我也需要分来问大家问题。。)。
[解决办法]
waitformutiobject的第3个参数,表示是否等待所有的handle都成为受信状态。函数才返回。
该函数等待的是1个handle数组,第1个参数代表该handle数组的handle数量。最后1个参数,代表超时时间。最多等多少时间就返回(即使handle还是未受信状态)。
多看看msdn吧,或者看看相关的技术书。这是一个非常基础的api

热点排行