Windows Mutual Exclusion and Threading Example in C++ by James Pate Williams, Jr.

// James Pate Williams, Jr. (c) 10/04/2022
// Second version, no longer dependent on
// a global object
#include <windows.h>
#include <synchapi.h>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std::chrono;
using namespace std;

// https://learn.microsoft.com/en-us/windows/win32/procthread/creating-threads
// https://www.geeksforgeeks.org/measure-execution-time-function-cpp/

long long counter;

class Test
{
public:
	inline void CountUp(
		HANDLE mutex,
		long long number)
	{
		counter = 0;

		for (long long i = 0; i < number; i++)
		{
			counter++;
		}

		ReleaseMutex(mutex);
	}
};

typedef struct Data
{
	long long number;
	DWORD dwWaitMS;
	Test* test;
} DATA, *PDATA;

DWORD WINAPI ThreadCode(LPVOID lpParam)
{
	Data* pData = (Data*)lpParam;
	HANDLE mutex = CreateMutex(NULL, FALSE, NULL);

	if (mutex != NULL)
	{
		pData->test->CountUp(
			mutex, pData->number);

		DWORD err = WaitForSingleObject(mutex, pData->dwWaitMS);
		CloseHandle(mutex);
	}

	return 0;
}

int main()
{
	long long number = 1000000000000;

	for (DWORD dwWaitMS = 30000;
		dwWaitMS <= 240000; dwWaitMS += 30000)
	{
		cout << "Number  = " << number << endl;
		cout << "Wait MS = " << dwWaitMS << endl;

		DWORD dwThreadId;
		Data* pData = new Data;

		pData->number = number;
		pData->dwWaitMS = dwWaitMS;
		pData->test = new Test();
		counter = 0;
		
		HANDLE hThread = NULL;

		hThread = CreateThread(
					NULL,			    // default security attributes
					0,					// use default stack size  
					ThreadCode,			// thread function name
					pData,				// argument to thread function 
					0,			        // use default creation flags 
					& dwThreadId);		// returns the thread identifier

		if (hThread != NULL)
		{
			auto start = high_resolution_clock::now();
			WaitForSingleObject(hThread, dwWaitMS);
			auto stop = high_resolution_clock::now();
			auto duration = duration_cast<milliseconds>(stop - start);
			double runtime = (double)duration.count() / 1.0e3;

			cout << "Counter = " << counter << endl;
			cout << "Runtime = " << runtime << endl;
			
			CloseHandle(hThread);
		}
	}
	return 0;
}
Number  = 1000000000000
Wait MS = 30000
Counter = 19226393400
Runtime = 30.006
Number  = 1000000000000
Wait MS = 60000
Counter = 7248012054
Runtime = 60.003
Number  = 1000000000000
Wait MS = 90000
Counter = 5264969696
Runtime = 90.011
Number  = 1000000000000
Wait MS = 120000
Counter = 10312016711
Runtime = 120
Number  = 1000000000000
Wait MS = 150000
Counter = 15342215540
Runtime = 150.011
Number  = 1000000000000
Wait MS = 180000
Counter = 20399182381
Runtime = 180
Number  = 1000000000000
Wait MS = 210000
Counter = 25358449910
Runtime = 210.009
Number  = 1000000000000
Wait MS = 240000
Counter = 30917682739
Runtime = 240.017

C:\Users\james\source\repos\MutexTest\Debug\MutexTest.exe (process 49516) exited with code 0.
Press any key to close this window . . .
Unknown's avatar

Author: jamespatewilliamsjr

My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.

Leave a comment