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(LCreating 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(LCreateEvent() #%d failed, error %d\n, i, GetLastError() );

            ExitProcess(0);

        }

            else

                  wprintf(LCreateEvent() #%d is OK!\n, i);

    }

 

    // Create a thread

    wprintf(LCreating 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(LCreateThread() failed, error %d\n, GetLastError());

        return;

    }

      else

            wprintf(LCreateThread() is OK!\n);

 

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

    wprintf(LWaiting 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(LFirst event was signaled...\n);

            break;

        // ghEvents[1] was signaled

        case WAIT_OBJECT_0 + 1:

            // TODO: Perform tasks required by this event

            wprintf(LSecond event was signaled...\n);

            break;

        case WAIT_TIMEOUT:

            wprintf(LWait timed out...\n);

            break;

        // Return value is invalid.

        default:

            wprintf(LWait failed with error %d\n, GetLastError());

            ExitProcess(0);

    }

 

    // Close event handles

      wprintf(LClosing the event handles...\n);

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

      {

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

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

            else

                  wprintf(LFail 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(LSetEvent() failed, error %d\n, GetLastError());

        return -1;

    }

      else

            wprintf(LSetEvent() 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 >