Notes on a Quick Performance Evaluation of std::mutex

Published on October 1, 2016

Notes on a Quick Performance Evaluation of std::mutex

These are quick notes on a performance evaluation of std::mutex.

Evaluation Method

A contention state for std::mutex is created by preparing 2 threads and using a std::atomic<bool> together with a short busy loop. The source code is available on gist (the computation time under contention can be calculated by subtracting LockAndUnlockWithPenalty from LockAndUnlockWithContention. On Linux the processing time is short even when contention occurs, so kShortWait was set to 30 during measurement in order to discern fine differences).
The evaluation machines are as follows.
  • Mac OSX 10.10.2 (3 GHz Intel Core i7)
  • Debian 3.16.7 x86_64 GNU/Linux (Intel(R) Xeon(R) CPU E3-1220 v5 @ 3.00GHz)

Performance Evaluation

Operation being evaluated Processing time on Mac Processing time on Linux
lock/unlock using std::atomic<bool> 14 ns 14 ns
lock/unlock using std::mutex without contention 55 ns 29 ns
lock/unlock using std::mutex with contention 6100 ns 68 ns
On Mac OSX, contention on std::mutex incurs a very large penalty, whereas on Linux there is a penalty but it was not particularly large.