Why std :: mutex is twice as slow as CRITICAL_SECTION

std :: mutex is implemented with critical partitions, so it is much faster than OS Mutex (on Windows). However, this is not as fast as Windows CRITICAL_SECTION.

Reading time only in a closed loop in one thread:

423.76ns ATL CMutex 41.74ns std::mutex 16.61ns win32 Critical Section 

My question is, what else does std :: mutex do? I looked at the source, but could not trace it. However, there were additional steps before he moved on to Crit Sec. My questions are: are these additional steps helpful? That is, what are the additional steps; What would I skip using CRITICAL_SECTION?

And why did they call it Mutex if it is not implemented using Mutex?

+8
c ++ mutex c ++ 11 winapi critical-section
source share
1 answer

A std :: mutex provides non-recursive property semantics. CRITICAL_SECTION provides recursive semantics. Therefore, I assume that an extra layer in the implementation of std :: mutex (at least in part) eliminates this difference.

Update. While executing the code, it seems that std :: mutex is implemented in terms of the queue and InterlockedX commands, and not in the classic Win32 CRITICAL_SECTION. Although std :: mutex is not recursive, the base code in RTL can handle recursive and even temporary locks.

+3
source share

All Articles