How best to test a Mutex implementation?

automated-tests, c++, concurrency, mutex, testing

Solution

This reminds me of this question about FIFO semaphore test. In a nutshell my answer was:

- Even if you have a specification, maybe it doesn't convey your intention exactly

- You can prove that the algorithm fulfills the specification, but not the code (D. Knuth)

- Test reveal only the presence of bug, not their absence (Dijkstra)

So your proposition seem reasonably the best to do. If you want to increase your confidence, use fuzzing to randomize scheduling and input.

Problem

What is the best way to test an implementation of a mutex is indeed correct? (It is necessary to implement a mutex, reuse is not a viable option) The best I have come up with is to have many (N) concurrent threads iteratively attempting to access the protected region (I) times, which has a side effect (e.g. update to a global) so that the number of accesses + writes can be counted to ensure that the number of updates to the global is exactly (N)*(I). Any other suggestions?

Original source