This is the fifth part of the Concurrency series. For your convenience you can find other parts in the table of contents in Part 1 – Mutex performance in .NET

Last time we called WinAPI through P/Invoke, today we are going to call it directly.

Code

#include "stdafx.h"
#include 
#include 
#include 

using namespace std;

int main()
{
	HANDLE mutex = CreateMutex(NULL, FALSE, L"mutex_cpp");

	while (true) {
		clock_t begin = clock();
		for (int i = 0;i < 100000;++i) {
			WaitForSingleObject(mutex, INFINITE);
			ReleaseMutex(mutex);
		}
		clock_t end = clock();
		double elapsed = double(end - begin) / CLOCKS_PER_SEC * 1000.0;
		printf("%lf\n", elapsed);
	}

	CloseHandle(mutex);

    return 0;
}

Results

The results are:

    \[ \begin{array}{cc} Number\ of\ processes & Time [ms] \\ 1 & 110\\ 2 & 605\\ 3 & 905\\ 4 & 1269\\ 5 & 1589\\ 6 & 1920\\ 7 & 2313\\ 8 & 2548\\ 9 & 2950\\ 10 & 3250\\ \end{array} \]

As you can see, the results are very similar to the P/Invoke ones.