peku33 peku33 - 1 month ago 14
C++ Question

Why shared_timed_mutex is defined in c++14, but shared_mutex in c++17?

C++11 introduced

std::mutex
and its extended version -
std::timed_mutex
.

However, in c++14 we have
std::shared_timed_mutex
, but its 'parent', the
std::shared_mutex
is going to be added in c++17.

Is there any reasonable explanation for that?

If I'm not going to use 'timed' functionality of
std::shared_timed_mutex
, is it going to be worse (slower, consuming more resources) than proposed
std::shared_mutex
?

Answer

Shared mutex originally had timing in it, and was called shared_mutex.

An implementor (msvc iirc) noted they could implement it "cheaper" without timing. In particular, SRWLOCK is an existing primitive on windows that is sufficient to implement shared mutex, but timed requires extra machinery. (Via @t.c.). (However, I believe it isn't just easier because already written, but also fundamentally more expensive, at least on x86/64 windows)

It was too late to add a new type to the standard, but not too late to rename it.

So it was renamed to shared_timed_mutex, and the untimed version added in next standard.

Here is at least one of the papers involved in the rename.

We propose to rename shared_mutex to shared_timed_mutex:

(a) for consistency with the other mutexes (fixing naming inconsistency);

(b) to leave room for a shared_mutex which can be more efficient on some platforms than shared_timed_mutex.