Karim Zaher
Karim Zaher

Reputation: 3

WaitForMultipleObjects and 2 Events when bWaitAll = true

I want to get something done after 2 events are set off; however, I am not sure what WaitForMultipleObjects will return when both events are set off.

This is what I have at the moment:

if (WaitForMultipleObjects(2, lpHandles, TRUE, 0) == ___)
{ 
   do stuff
} 
else 
{
    continue
}

What am I supposed to place in the ___ field? I'm fairly new to the WinAPI and the MSDN documentation was slightly confusing to me.

Upvotes: 0

Views: 149

Answers (2)

Drake Wu
Drake Wu

Reputation: 7210

When dwMilliseconds is specified as 0, if the specified objects are not signaled, the function returns WAIT_TIMEOUT immediately. And if all specified objects are signaled, according to the document, the function return WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1):

DWORD WINAPI PROCESST1(LPVOID param)
{
    int index1 = 10;
    while (index1--)
    {
        cout << "In The Thread1 , ID = " << GetCurrentThreadId() << endl;
        Sleep(1000);
    }
    return 0;
}

DWORD WINAPI PROCESST2(LPVOID param)
{
    int index1 = 10;
    while (index1--)
    {
        cout << "In The Thread2 , ID = " << GetCurrentThreadId() << endl;
        Sleep(1000);
    }
    return 0;
}


int main(int argc, TCHAR* argv[])
{
    HANDLE hThread[2] = { 0 };
    DWORD thread1_id, thread2_id;
    hThread[0] = CreateThread(NULL, 0, PROCESST1, 0, 0, &thread1_id);
    Sleep(5000);
    hThread[1] = CreateThread(NULL, 0, PROCESST2, 0, 0, &thread2_id);
    DWORD ret;
    while (true)
    {
        Sleep(500);
        ret = WaitForMultipleObjects(2, hThread, TRUE, 0);
        if (ret >= WAIT_OBJECT_0 && ret <= WAIT_OBJECT_0 +2 -1)
        {
            cout << ret << endl;
            break;
        }
        else
        {
            cout << ret << endl;
            continue;
        }
    }
    CloseHandle(hThread[0]);
    CloseHandle(hThread[1]);
    system("pause");
    return 0;
}

Result:

enter image description here

Upvotes: 0

SoronelHaetir
SoronelHaetir

Reputation: 15182

When bWaitAll is TRUE then a return value of WAIT_OBJECT_0 indicates that all passed handles are in the signaled state. You only get a varying object indicated when bWaitAll is not TRUE so that WFMO returns with only one object signaled.

Upvotes: 1

Related Questions