Windows Thread Synchronization 21

 

 

 

Another Mutex Program Example

 

The following program demonstrates an unnamed mutex object used to have mutual exclusive access to a global variable shared between three threads.  The main thread first creates a mutex object and two child threads.  Each child threads basically try getting the ownership of the unnamed mutex object. When owner ship is acquired by one of the child threads, it sets and resets the shared global variable.  When the main thread acquires ownership of the mutex object, it prints out the value of the shared global variable.  The output should always print the out value of the shared global variable.

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

 

Another Mutex Program Example: Creating new C++ Win32 console application project

 

 

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

 

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

 

Next, add the following source code.

 

// Example of Unnamed Mutex object

#include <windows.h>

#include <stdio.h>

 

// Global shared hMutex Object

HANDLE hMutex;

DWORD SharedVal = 0;

 

////////////// Thread counts till MaxValue

void ThreadMain(void)

{

      static DWORD count = 0;

 

      for(;;)

      {

        // wait for ownership

        WaitForSingleObject(hMutex,INFINITE);

        SharedVal += 20;

        // sleep for 100ms

        Sleep(100);

        // So, each iteration will add 1 (20 - 19) for each thread...

        SharedVal -= 19;

 

            if(ReleaseMutex(hMutex)  != 0)

                  wprintf(L ThreadMain() - ReleaseMutex() is OK!\n);

            else

                  wprintf(L ThreadMain() - ReleaseMutex() failed, error %u\n, GetLastError());

            count++;

            wprintf(LCount value #%d\n, count);

      }

}

 

///////////////////////////////////////////////

int wmain(void)

{

      HANDLE hThr;

      DWORD dwThreadId, i;

     

      // unnamed mutex object

      hMutex=CreateMutex(NULL,FALSE,NULL);

 

      if(hMutex != NULL)

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

     

      // Create Two Threads, a thread to execute within

      // the virtual address space of the calling process

      hThr=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,NULL,0,&dwThreadId);

 

      if(hThr != NULL)

            wprintf(L CreateThread() is OK! Thread ID %u\n, dwThreadId);

      else

            wprintf(L CreateThread() failed, error %u!\n, GetLastError());

 

    if(CloseHandle(hThr) != 0)

            wprintf(L CreateThread() hThr handle was closed successfully!\n);

      else

            wprintf(L Failed to close hThr handle, error %u!\n, GetLastError());

 

 

    hThr=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,NULL,0,&dwThreadId);

            if(hThr != NULL)

            wprintf(L CreateThread() is OK! Thread ID %u\n, dwThreadId);

      else

            wprintf(L CreateThread() failed, error %u!\n, GetLastError());

 

    if(CloseHandle(hThr) != 0)

            wprintf(L CreateThread() hThr handle was closed successfully!\n);

      else

            wprintf(L Failed to close hThr handle, error %u!\n, GetLastError());

     

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

      {

        // Wait for ownership

        WaitForSingleObject(hMutex,INFINITE);

        wprintf(L#%d Value of SharedVal = %d\n, i, SharedVal);

       

            if(ReleaseMutex(hMutex)  != 0)

                  wprintf(L main() - ReleaseMutex() is OK!\n);

            else

                  wprintf(L main() - ReleaseMutex() failed, error %u\n, GetLastError());

      }

 

      return 0;

    }

 

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

 

Another Mutex Program Example: A sample of console application program output

 

 

 

< Thread Synchronization 20 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 22 >