Is C++17 std::shared_mutex not available yet?
Asked Answered
M

2

13

Looking at C++ compiler support, it appears that the untimed version of std::shared_mutex is available in GCC 5.0+. However, even with gcc version 5.3.0 20151204 (Ubuntu 5.3.0-3ubuntu1~14.04), and compiling with -std=c++1z, a simple initialization of a shared mutex ends up with:

error: ‘shared_mutex’ in namespace ‘std’ does not name a type
        std::shared_mutex mutex_;

And no, I have already included the proper header: #include <shared_mutex>.

It can't locate the proper header, because it does not seem to exist. Actually, the linker uses the library locate at /usr/include/c++/5/shared_mutex, which contains only the implementation of the std::shared_timed_mutex (like the C++14 standard).

I have installed gcc-5 and g++-5 by adding the repository at ppa:ubuntu-toolchain-r/test and using update-alternatives to properly set up their bins.

Is there something I can do to compile my code correctly using the newest C++17 standard? And probably is a stupid question to ask, but is it too early to start using -std=c++1z even if it should be already supported? Because it is supported, right?

Mulry answered 25/3, 2016 at 15:31 Comment(9)
C++17 does not yet exist. Expecting to have implementations of features that do not yet exist and are not yet in a complete version of the language is... well, it kinda speaks for itself.Selfpossession
Thanks @NicolBolas, I see. So the green marker in the link above is just smoke and mirrors, I guess.Mulry
If you're open to switching to a different standard library implementation, libc++ claims to have support for shared_mutex.Turnstile
According to this resource, the implementation should be in trunk, not necessarily in gcc5 itselfWatery
@NicolBolas it's legitimate to expect experimental implementation/support for features that have already been voted in, before the standard document is officially released in its final formWatery
Many thanks for the clarification @PiotrSkotnicki and for the suggestion, Michael: I'll check if it works as expected or I'll directly change approach.Mulry
cppreference fixed. This was committed well after GCC 5.1's release, so there's no way it's going to be in the 5 branch.Gilbertina
@Gilbertina I had already seen it yesterday, but I had no time to post it here. +1 for fixing the reference.Mulry
you can use boost::shared_mutex instead of std::shared_mutex.Janeanjaneczka
H
18

The confusion on cppreference was probably because std::shared_mutex really was added to GCC 5.0, in revision 200134. But that was the early incarnation of that type based on a C++1y draft. In fact it was the timed shared mutex, which was called std::shared_mutex at the time.

Before the final C++14 standard was published std::shared_mutex was renamed to std::shared_timed_mutex, and so before the GCC 5.1 release (which is the first release in the 5.x series) the type in libstdc++ was renamed, see revision 207964.

So although at one point during the GCC 5.x pre-release phase there was a std::shared_mutex type, it wasn't the C++17 untimed one, and it got renamed before appearing in any official release of GCC.

Then, during the development of the GCC 6.x release series the C++1z untimed shared mutex got added, reusing the std::shared_mutex name. That's the commit T.C. linked to in the comments above, revision 224158.

So the C++17 untimed shared_mutex was never in any GCC 5.x version. For a brief period before the first 5.x release there was a timed one called std::shared_mutex, but in all proper 5.x releases it's called std::shared_timed_mutex.

The first release to ship the C++17 untimed one was 6.1 in April 2016, so with any GCC release after that you can use std::shared_mutex (as long as you enable C++17 in the compiler, e.g. with the -std=gnu++17 or -std=c++17 flag).

GCC 5 was released in 2015, so expecting to be able to use C++17 with that version is a bit unrealistic. GCC 6.x and 7.x have pretty good C++1z support (but only based on the current drafts at the time of release, of course).

Hans answered 17/2, 2017 at 15:54 Comment(5)
Thanks for the clarification. I've marked the answer as accepted even though it has been almost answered in the comments above.Mulry
there is know answer, i have the include but it still saying error: ‘shared_mutex’ in namespace ‘std’ does not name a type, when i still have the include, so weird, long answer, no solutionProsecutor
@PatrikLaszlo did you actually read the answer? It explains that in GCC 5.x releases the <shared_mutex> header might only provide std::shared_timed_mutex and not std::shared_mutex. That was the whole point of the question: having the header doesn't mean you have the type. If you have GCC 6.1 or later and still get the error then it's probably because you're not telling the compiler to use C++17, try adding -std=c++17 or -std=gnu++17.Hans
OHHH! THANKS SO MUCH! WORKS RIGHT AWAY! I WAS ON C++11, now ON CMAKE: set(CMAKE_CXX_STANDARD 17)Prosecutor
Obviously if you want to use new type that was added in C++17 type then you need to use C++17.Hans
F
0

follow this link to install/upgrade to the latest version of GCC and G++. http://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu

I have tried it on my ubuntu and successful.

Faultfinder answered 27/5, 2019 at 15:47 Comment(2)
The question was posted before the final release of the C++17 standard. It's a nonsense now.Mulry
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Stillbirth

© 2022 - 2024 — McMap. All rights reserved.