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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include "stdafx.h" #include <windows.h> #include <ctime> #include <cstdio> 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:
As you can see, the results are very similar to the P/Invoke ones.