Windows Thread Synchronization 43

 

 

 

 

 

Synchronization Using Mutexes Program Example

 

A mutex is a kernel object that provides a thread with mutually exclusive access to a single resource. The state of a mutex object is set to signaled when it is not owned by any thread, and nonsignaled when it is owned. Only one thread at a time can own a mutex object, whose name comes from the fact that it is useful in coordinating mutually exclusive access to a shared resource. Any thread of the calling process can specify the mutex-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. When the state of the mutex is signaled, one waiting thread is granted ownership, the state of the mutex changes to nonsignaled, and the wait function returns. The owning thread uses the ReleaseMutex function to release its ownership. The next example looks at the use of mutexes to coordinate access to a shared resource and to handshake between two threads.

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

 

Synchronization Using Mutexes Program Example: Creating another Win32 console application project

 

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

 

Synchronization Using Mutexes Program Example: Adding new C++ source file

 

Next, add the following source code.

 

#include <Windows.h>

#include <stdio.h>

 

#define SHARED_SIZE 1024

 

// Shared global variables

WCHAR shared_area[SHARED_SIZE];

LPCTSTR lpszMutex = L"NamedMutex";

HANDLE shared_mutex;

 

DWORD WINAPI thread_function(LPVOID arg)

{

      DWORD dwWfSO, dwWfSO1;

 

      // Opens an existing named mutex object.

      HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, lpszMutex);

      if(hMutex == NULL)

            wprintf(L"OpenMutex() failed, error %u\n", GetLastError());

      else

            wprintf(L"%s mutex handle successfully opened!\n", lpszMutex);

 

      dwWfSO = WaitForSingleObject(hMutex, INFINITE);

      wprintf(L"WaitForSingleObject() 1 returned value is 0X%.8X\n", dwWfSO);

     

      while(wcsncmp(L"done", shared_area, 4) != 0)

      {

            wprintf(L"You input %d characters...\n", wcslen(shared_area) -1);

            // Releases ownership of the specified mutex object.

            // If the function succeeds, the return value is nonzero.

            // If the function fails, the return value is zero.

            if(ReleaseMutex(hMutex) != 0)

                  wprintf(L"hMutex handle was released!\n");

            else

                  wprintf(L"Failed to release the hMutex handle, error %u\n", GetLastError());

 

            dwWfSO1 = WaitForSingleObject(hMutex, INFINITE);

            wprintf(L"WaitForSingleObject() 2 returned value is 0X%.8X\n", dwWfSO1);

      }

     

      if(ReleaseMutex(hMutex) != 0)

            wprintf(L"hMutex handle was released!\n");

      else

            wprintf(L"Failed to release the hMutex handle, error %u\n", GetLastError());

      if(CloseHandle(hMutex) != 0)

            wprintf(L"hMutex handle was closed successfully!\n");

      else

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

      return 0;

}

int wmain()

{

      HANDLE a_thread;

      DWORD a_threadId, dwMWfSO1, dwMWfSO2;

      DWORD thread_result;

     

      // Create a mutex with no initial owner

      shared_mutex = CreateMutex( NULL, TRUE, lpszMutex );

      if (shared_mutex == NULL)

      {

            wprintf(L"CreateMutex() - Mutex initialization failed, error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"CreateMutex() is OK! A Mutex was created successfully!\n");

 

      // Create a new thread.

      a_thread = CreateThread(NULL, 0, thread_function, (LPVOID)NULL, 0,&a_threadId);

 

      if (a_thread == NULL)

      {

            wprintf(L"CreateThread() - Thread creation failed, error %u", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"CreateThread() is OK, thread ID is %u\n", a_threadId);

     

      wprintf(L"\nInput some text. Enter 'done' to finish\n");   

      wprintf(L"\n");

 

      while(wcsncmp(L"done", shared_area, 4) != 0)

      {

            fgetws(shared_area, SHARED_SIZE, stdin);

 

            if(ReleaseMutex(shared_mutex) != 0)

                  wprintf(L"shared_mutex handle was released!\n");

            else

                  wprintf(L"Failed to release the shared_mutex handle, error %u\n", GetLastError());

 

            dwMWfSO1 = WaitForSingleObject(shared_mutex, INFINITE);

            wprintf(L"WaitForSingleObject() 3 returned value is 0X%.8X\n", dwMWfSO1);

            wprintf(L"\n");

      }

     

      if(ReleaseMutex(shared_mutex) != 0)

            wprintf(L"shared_mutex handle was released!\n");

      else

            wprintf(L"Failed to release the shared_mutex handle, error %u\n", GetLastError());

 

      wprintf(L"Waiting for thread to finish...\n");

 

      dwMWfSO2 = WaitForSingleObject(a_thread, INFINITE);

      wprintf(L"WaitForSingleObject() 4 returned value is 0X%.8X\n", dwMWfSO1);

     

      if(dwMWfSO2 != WAIT_OBJECT_0)

      {

            wprintf(L"WaitForSingleObject() failed, thread join failed, error %u\n", GetLastError());

            exit(EXIT_FAILURE);

      }

      else

            wprintf(L"WaitForSingleObject() 4 is OK!\n");

 

      // Retrieve the code returned by the thread.

      if(GetExitCodeThread(a_thread, &thread_result) != 0)

            wprintf(L"GetExitCodeThread() is OK! Thread joined, exit code %u\n", thread_result);

      else

            wprintf(L"GetExitCodeThread() failed, error %u\n", GetLastError());

 

      if(CloseHandle(shared_mutex) != 0)

            wprintf(L"shared_mutex handle was closed successfully!\n");

      else

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

     

      return 0;

}

 

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

 

 

Synchronization Using Mutexes Program Example: A sample console program output

 

 

 

 

< Thread Synchronization 42 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 44 >