Windows Thread Synchronization 36

 

 

 

The Interlocked Functions Program Example 2

 

The next program prints out the total number of threads that are currently in a certain critical section of code.  When a thread is entering the critical section, it uses InterlockedIncrement() to increase a global variable.  When the thread is about to leave the critical section code, it uses the function InterlockedDecrement() to decrease the global variable.  There are only three threads that could be in the critical section. If you replace the InterlockedIncrement() function with a regular increment statement and the InterlockedDecrement() function for a decrement statement, you will start to see weird numbers for the total number of threads in the critical section.

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

 

The Interlocked Functions Program Example 2: Creating new C++ empty Win32 console application project

 

 

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

 

The Interlocked Functions Program Example 2: Adding new C++ source file

 

Next, add the following source code.

 

// InterlockedIncrement() and InterlockedDecrement() program example

#include <windows.h>

#include <stdio.h>

 

volatile LONG TotalThreadCount = 0;

 

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

void ThreadMain(void)

{

      DWORD dwIncre, dwDecre;

 

      for(;;)

      {

            // A critical section begins

            // Increments (increases by one) the value of the specified 32-bit variable as an atomic operation.

            // The function returns the resulting incremented value.

            dwIncre = InterlockedIncrement((LPLONG)&TotalThreadCount);

           

            wprintf(LInterlockedIncrement() - Increment value is %u\n, dwIncre);

 

            // Dummy, time to complete task...

            // Should be random

            // Sleep(100);

 

            // critical section ends

            // Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation.

            // The function returns the resulting decremented value.

            dwDecre = InterlockedDecrement((LPLONG)&TotalThreadCount);

            wprintf(LInterlockedDecrement() - Decrement value is %u\n, dwDecre);

 

            // Another dummy,  giving some time, should be random

            // Sleep(rand()%1000);

      }

}

 

///////// Create A Child //////////////

void CreateChildThread(void)

{

      HANDLE hThread;

      DWORD dwId;

 

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

 

      if(hThread != NULL)

            wprintf(LCreateThread() is OK, thread ID is %u\n, dwId);

      else

            wprintf(LCreateThread() failed, error %u\n, GetLastError());

 

      wprintf(L\n);

 

      if(CloseHandle(hThread) != 0)

            wprintf(LhThread's handle closed successfully!\n);

      else

            wprintf(LFailed to close hThread's handle, error %u\n, GetLastError());

 

      wprintf(L\n);

}

 

///// Main ////////

int wmain(void)

{

      DWORD Count;

 

      CreateChildThread();

      CreateChildThread();

      CreateChildThread();

 

      // May test larger range...

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

      {

            wprintf(L Total number of threads in critical section = %u\n, TotalThreadCount);

      }

      return 0;

}

 

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

 

The Interlocked Functions Program Example 2: A sample output in action

 

 

 

< Thread Synchronization 35 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 37 >