Windows Thread Synchronization 16

 

 

 

 

 

Waiting for Multiple Objects Example

 

The following example uses the CreateEvent() function to create two event objects and the CreateThread() function to create a thread. It then uses the WaitForMultipleObjects() function to wait for the thread to set the state of one of the objects to signaled using the SetEvent() function.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Waiting for Multiple Objects Example: Creating new Win32 empty console mode application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

Waiting for Multiple Objects Example: Adding new C++ source file for the source code

 

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

HANDLE ghEvents[2];

 

DWORD WINAPI ThreadProc(LPVOID);

 

void wmain()

{

    HANDLE hThread;

    DWORD i, dwEvent, dwThreadID;

 

    // Create two event objects

    wprintf(L"Creating event objects...\n");

 

    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)

        {

                  wprintf(L"CreateEvent() #%d failed, error %d\n", i, GetLastError() );

            ExitProcess(0);

        }

            else

                  wprintf(L"CreateEvent() #%d is OK!\n", i);

    }

 

    // Create a thread

    wprintf(L"Creating a thread...\n");

    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 )

    {

        wprintf(L"CreateThread() failed, error %d\n", GetLastError());

        return;

    }

      else

            wprintf(L"CreateThread() is OK!\n");

 

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

    wprintf(L"Waiting the thread to signal one of the event objects...\n");

 

    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

            wprintf(L"First event was signaled...\n");

            break;

        // ghEvents[1] was signaled

        case WAIT_OBJECT_0 + 1:

            // TODO: Perform tasks required by this event

            wprintf(L"Second event was signaled...\n");

            break;

        case WAIT_TIMEOUT:

            wprintf(L"Wait timed out...\n");

            break;

        // Return value is invalid.

        default:

            wprintf(L"Wait failed with error %d\n", GetLastError());

            ExitProcess(0);

    }

 

    // Close event handles

      wprintf(L"Closing the event handles...\n");

    for (i = 0; i < 2; i++)

      {

            if(CloseHandle(ghEvents[i]) != 0)

                  wprintf(L"Event's handle #%d was successfully closed...\n", i);

            else

                  wprintf(L"Fail to close event's handle #%d, error %d\n", i, GetLastError());

      }

}

 

DWORD WINAPI ThreadProc( LPVOID lpParam )

{

    // Set one event to the signaled state

    if ( !SetEvent(ghEvents[0]) )

    {

        wprintf(L"SetEvent() failed, error %d\n", GetLastError());

        return -1;

    }

      else

            wprintf(L"SetEvent() is OK!\n");

 

    return 1;

}

 

Build and run the project. The following screenshot is a sample output.

 

Waiting for Multiple Objects Example: A sample console program output in action

 

 

 

 

< Thread Synchronization 15 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 17 >