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;




            // 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(L"InterlockedIncrement() - 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(L"InterlockedDecrement() - 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(L"CreateThread() is OK, thread ID is %u\n", dwId);


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




      if(CloseHandle(hThread) != 0)

            wprintf(L"hThread's handle closed successfully!\n");


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





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

int wmain(void)


      DWORD Count;






      // 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 >