TL;DR: unfortunate combination of backward compatibility and ABI compatibility issues makes std::mutex bad until the next ABI break. OTOH, std::shared_mutex is good.
A decent implementation of std::mutex would try to use an atomic operation to acquire the lock, if busy, possibly would try spinning in a read loop (with some pause on x86), and ultimately will resort to OS wait.
There are a couple of ways to implement such std::mutex:
- Directly delegate to corresponding OS APIs that do all of above.
- Do spinning and atomic thing on its own, call OS APIs only for OS wait.
Sure, the first way is easier to implement, more friendly to debug, more robust. So it appears to be the way to go. The candidate APIs are:
- CRITICAL_SECTIONAPIs. A recursive mutex, that is lacking static initializer and needs explicit destruction
- SRWLOCK. A non-recursive shared mutex that has static initializer and doesn't need explicit destruction
- WaitOnAddress. An API to wait on particular variable to be changed, similar to Linux- futex.
These primitives have OS version requirements:
- CRITICAL_SECTIONexisted since I think Windows 95, though- TryEnterCriticalSectionwas not present in Windows 9x, but the ability to use- CRITICAL_SECTIONwith- CONDITION_VARIABLEwas added since Windows Vista, with- CONDITION_VARIABLEitself.
- SRWLOCKexists since Windows Vista, but- TryAcquireSRWLockExclusiveexists since Windows 7, so it can only directly implement- std::mutexstarting in Windows 7.
- WaitOnAddresswas added since Windows 8.
By the time when std::mutex was added, Windows XP support by Visual Studio C++ library was needed, so it was implemented using doing things on its own. In fact, std::mutex and other sync stuff was delegated to ConCRT (Concurrency Runtime)
For Visual Studio 2015, the implementation was switched to use the best available mechanism, that is SRWLOCK starting in Windows 7, and CRITICAL_SECTION stating in Windows Vista. ConCRT turned out to be not the best mechanism, but it still was used for Windows XP and 2003. The polymorphism was implemented by making placement new of classes with virtual functions into a buffer provided by std::mutex and other primitives.
Note that this implementation breaks the requirement for std::mutex to be constexpr, because of runtime detection, placement new, and inability of pre-Window 7 implementation to have only static initializer.
As time passed support of Windows XP was finally dropped in VS 2019, and support of Windows Vista was dropped in VS 2022, the change is made to avoid ConCRT usage, the change is planned to avoid even runtime detection of SRWLOCK (disclosure: I've contributed these PRs). Still due to ABI compatibility for VS 2015 though VS 2022 it is not possible to simplify std::mutex implementation to avoid all this putting classes with virtual functions.
What is more sad, though SRWLOCK has static initializer, the said compatibility prevents from having constexpr mutex: we have to placement new the implementation there. It is not possible to avoid placement new, and make an implementation to construct right inside std::mutex, because std::mutex has to be standard layout class (see Why is std::mutex a standard-layout class?).
So the size overhead comes from the size of ConCRT mutex.
And the runtime overhead comes from the chain of call:
- library function call to get to the standard library implementation
- virtual function call to get to SRWLOCK-based implementation
- finally Windows API call.
Virtual function call is more expensive than usually due to standard library DLLs being built with /guard:cf.
Some part of the runtime overhead is due to std::mutex fills in ownership count and locked thread. Even though this information is not required for SRWLOCK. It is due to shared internal structure with recursive_mutex. The extra information may be helpful for debugging, but it does take time to fill it in.
std::shared_mutex was designed to support only systems starting Windows 7. So it uses SRWLOCK directly.
The size of std::shared_mutex is the size of SRWLOCK. SRWLOCK has the same size as a pointer (though internally it is not a pointer).
It still involves some avoidable overhead: it calls C++ runtime library, just to call Windows API, instead of calling Windows API directly. This looks fixable with the next ABI, though.
std::shared_mutex constructor could be constexpr, as SRWLOCK does not need dynamic initializer, but the standard prohibits voluntary adding constexpr to the standard classes.