1

如果我使用的是 WaitForMultipleObjects,并且该函数返回 WAIT_TIMEOUT,我如何获取哪些对象或哪些对象导致超时发生?

我的另一个问题是,如果多个对象发出信号,因为返回值只返回它检测为发出信号的第一个对象,我如何获得其他发出信号的对象?

#include <windows.h>
#include <stdio.h>

HANDLE ghEvents[2];

DWORD WINAPI ThreadProc( LPVOID );

int main( void )
{
    HANDLE hThread; 
    DWORD i, dwEvent, dwThreadID; 

    // Create two event objects

    for (i = 0; i < 2; i++) 
    { 
        ghEvents[i] = CreateEvent( 
            NULL,   // default security attributes
            FALSE,  // auto-reset event object
            FALSE,  // initial state is nonsignaled
            NULL);  // unnamed object

        if (ghEvents[i] == NULL) 
        { 
            printf("CreateEvent error: %d\n", GetLastError() ); 
            ExitProcess(0); 
        } 
    } 

    // Create a thread

    hThread = CreateThread( 
                 NULL,         // default security attributes
                 0,            // default stack size
                 (LPTHREAD_START_ROUTINE) ThreadProc, 
                 NULL,         // no thread function arguments
                 0,            // default creation flags
                 &dwThreadID); // receive thread identifier

    if( hThread == NULL )
    {
        printf("CreateThread error: %d\n", GetLastError());
        return 1;
    }

    // Wait for the thread to signal one of the event objects

    dwEvent = WaitForMultipleObjects( 
        2,           // number of objects in array
        ghEvents,     // array of objects
        FALSE,       // wait for any object
        5000);       // five-second wait

    // The return value indicates which event is signaled

    switch (dwEvent) 
    { 
        // ghEvents[0] was signaled
        case WAIT_OBJECT_0 + 0: 
            // TODO: Perform tasks required by this event
            printf("First event was signaled.\n");
            break; 

        // ghEvents[1] was signaled
        case WAIT_OBJECT_0 + 1: 
            // TODO: Perform tasks required by this event
            printf("Second event was signaled.\n");
            break; 

        case WAIT_TIMEOUT:
            // How can I get which object timed out?
            printf("Wait timed out.\n");
            break;

        // Return value is invalid.
        default: 
            printf("Wait error: %d\n", GetLastError()); 
            ExitProcess(0); 
    }

    // Close event handles

    for (i = 0; i < 2; i++) 
        CloseHandle(ghEvents[i]); 

    return 0;   
}

DWORD WINAPI ThreadProc( LPVOID lpParam )
{

    // lpParam not used in this example
    UNREFERENCED_PARAMETER( lpParam);

    // Set one event to the signaled state

    if ( !SetEvent(ghEvents[0]) ) 
    {
        printf("SetEvent failed (%d)\n", GetLastError());
        return 1;
    }
    return 0;
}
4

1 回答 1

8

WaitForMultipleObjects(...)返回WAIT_TIMEOUT代码返回时,它表明您等待的任何对象都没有在给定的时间内发出信号。

该函数本质上会在您指定为超时的时间内休眠,并且仅在可等待对象之一在该时间之前发出信号时才更早返回。这意味着WAIT_TIMEOUT返回码与您等待的任何对象都没有关联。

Eregiths 的评论部分回答了您的第二个问题。要检查是否也向其他对象发出信号,您可以WaitForMultipleObjects(...)再次调用,并根据您的需要,将超时值设置为0(不要等待)。当与您WaitForMultipleObjects(...)一起返回时,WAIT_TIMEOUT您知道在您调用时没有其他对象处于信号状态,但您应该记住,导致您第一次调用返回的对象可能会再次发出信号。因此,您可以将其从数组中排除,也可以简单地使用该函数检查单个对象的状态WaitForSingleObject(...)

如果您想确保所有对象都发出信号,您也可以使用该bWaitAll参数。WaitForMultipleObjects(...)只有当你的所有对象都处于信号状态时才会返回。,

希望有点帮助。

于 2012-06-15T10:02:23.307 回答