Ran the following in Visual Studio 2022 in release mode:
#include <chrono>
#include <mutex>
#include <shared_mutex>
#include <iostream>
std::mutex mx;
std::shared_mutex smx;
constexpr int N = 100'000'000;
int main()
{
auto t1 = std::chrono::steady_clock::now();
for (int i = 0; i != N; i++)
{
std::unique_lock<std::mutex> l{ mx };
}
auto t2 = std::chrono::steady_clock::now();
for (int i = 0; i != N; i++)
{
std::unique_lock<std::shared_mutex> l{ smx };
}
auto t3 = std::chrono::steady_clock::now();
auto d1 = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
auto d2 = std::chrono::duration_cast<std::chrono::duration<double>>(t3 - t2);
std::cout << "mutex " << d1.count() << "s; shared_mutex " << d2.count() << "s\n";
std::cout << "mutex " << sizeof(mx) << " bytes; shared_mutex " << sizeof(smx) << " bytes \n";
}
The output is as follows:
mutex 2.01147s; shared_mutex 1.32065s
mutex 80 bytes; shared_mutex 8 bytes
Why so?
It is unexpected that more rich in features std::shared_mutex
is faster than std::mutex
, which is strictly a subset in its features.
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
:
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_SECTION
APIs. A recursive mutex, that is lacking static initializer and needs explicit destructionSRWLOCK
. A non-recursive shared mutex that has static initializer and doesn't need explicit destructionWaitOnAddress
. An API to wait on particular variable to be changed, similar to Linux futex
.These primitives have OS version requirements:
CRITICAL_SECTION
existed since I think Windows 95, though TryEnterCriticalSection
was not present in Windows 9x, but the ability to use CRITICAL_SECTION
with CONDITION_VARIABLE
was added since Windows Vista, with CONDITION_VARIABLE
itself.SRWLOCK
exists since Windows Vista, but TryAcquireSRWLockExclusive
exists since Windows 7, so it can only directly implement std::mutex
starting in Windows 7.WaitOnAddress
was 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:
SRWLOCK
-based implementationVirtual 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With