Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C++17 std::shared_mutex not available yet?

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?

like image 238
alextoind Avatar asked Mar 25 '16 15:03

alextoind


People also ask

What is shared_mutex?

The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.

Is shared_mutex recursive?

Shared recursive mutexThe C++ 17 standard defines a shared_mutex, which is another term for reader/writer lock and a recursive mutex which is a mutex which you can lock multiple times from the same thread.

When to use shared_ mutex?

"Shared mutexes are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so."

What is Shared_timed_mutex?

The class shared_timed_mutex is a shared timed mutex type, a type that meets the requirements of both a shared mutex type and a timed mutex type.


1 Answers

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).

like image 141
Jonathan Wakely Avatar answered Oct 20 '22 14:10

Jonathan Wakely