This is the third 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 saw file lock in Java which we can use on every platform. However, if we stick to Windows only, we can use WinAPI mutexes through JNI. Let’s go.


First, Java application:

We define three methods to use Mutex. Observe that we use long instead of HANDLE. HANDLE is a void* which we can consider a number in our case.

Next, we generate the JNI header:

and here is the result:

Now we implement the methods in C++ with Visual Studio 2017:

Compile, run with -Djava.library.path="..." and test.


    \[ \begin{array}{cc} Number\ of\ processes & Time [ms] \\ 1 & 80\\ 2 & 575\\ 3 & 892\\ 4 & 1244\\ 5 & 1502\\ 6 & 1886\\ 7 & 2239\\ 8 & 2593\\ 9 & 2865\\ 10 & 3175\\ \end{array} \]

As we can see, the results are quite nice, even better than .NET. Next time we will see if we can improve C# code.