Windows Thread Synchronization 28

 

 

 

 

 

Another Critical Section Program Example

 

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

 

Another Critical Section Program Example: Creating new C++ Win32 console application

 

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

 

Another Critical Section Program Example: Adding new C++ source file to the existing project

 

Next, add the following source code.

 

// The following program demonstrates an example of using an critical section object

#include <windows.h>

#include <stdio.h>

 

// Global variable

CRITICAL_SECTION criticalSectionSample;

 

///////////// Thread Main ///////////////////////

void ThreadMain(char *threadNum)

{

        // Waits for ownership of the specified critical section object.

        // The function returns when the calling thread is granted ownership.

        // This function does not return a value.

        wprintf(L"\nEnterCriticalSection() - entering the critical section...\n");

        EnterCriticalSection(&criticalSectionSample);

 

        wprintf(L"%S in critical section, using the shared resource...\n", threadNum);

      

        // Releases ownership of the specified critical section object.

        // This function does not return a value.

        wprintf(L"\nLeaveCriticalSection() - Leaving the critical section...\n");

        LeaveCriticalSection(&criticalSectionSample);

}

 

/////////////Creating A Child//////////////

HANDLE CreateChildTh(char *threadNum)

{

      HANDLE hThread;

      DWORD dwId;

 

      hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)threadNum,0,&dwId);

     

      if(hThread)

      {

            wprintf(L"CreateThread() is OK, ID num %d...\n", dwId);

            return hThread;

      }

      else

      {

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

            return NULL;

      }

}

 

////////////// The main() ///////////////////////

int wmain(void)

{

    HANDLE hThreadHandle[3];

    DWORD dwEvent, i;

 

    // Initializes a critical section object.

    // This function does not return a value.

    wprintf(L"Initializing the critical section...\n");

    InitializeCriticalSection(&criticalSectionSample);

 

    hThreadHandle[0] = CreateChildTh("ChildThread1");

    hThreadHandle[1] = CreateChildTh("ChildThread2");

    hThreadHandle[2] = CreateChildTh("ChildThread3");

  

    // Waits until one or all of the specified objects are

    // in the signaled state or the time-out interval elapses.

    dwEvent = WaitForMultipleObjects(3,hThreadHandle,TRUE,INFINITE);

 

      switch (dwEvent)

    {

        // hThreadHandle[0] was signaled

        case WAIT_OBJECT_0 + 0:

            // TODO: Perform tasks required by this event

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

            break;

 

        // hThreadHandle[1] was signaled

        case WAIT_OBJECT_0 + 1:

            // TODO: Perform tasks required by this event

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

            break;

 

        // hThreadHandle[2] was signaled

        case WAIT_OBJECT_0 + 2:

            // TODO: Perform tasks required by this event

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

            break;

 

        case WAIT_TIMEOUT:

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

            break;

 

        // Return value is invalid.

        default:

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

            ExitProcess(0);

    }

 

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

   {

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

            wprintf(L"Closing the hThreadHandle[%d] handle is OK...\n", i);

      else

            wprintf(L"Failed to close the hThreadHandle[%d] handle, error %d...\n", GetLastError());

   }

 

    // Releases all resources used by an unowned critical section object.

    // This function does not return a value.

    wprintf(L"DeleteCriticalSection() - Releasing all resources used\n by an unowned critical section object...\n");

    DeleteCriticalSection(&criticalSectionSample);

    return 0;

}

 

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

 

Another Critical Section Program Example: A sample console mode program output

 

 

 

 

< Thread Synchronization 27 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 29 >