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:
![Rendered by QuickLaTeX.com \[ \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} \]](../../../../wp-content/ql-cache/quicklatex.com-7bcc7978f4b1e9f7bda4c1ce7085a01f_l3.png)
As you can see, the results are very similar to the P/Invoke ones.