11

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?

4

2 回答 2

17

对 cppreference 的混淆可能是因为std::shared_mutex真的添加到 GCC 5.0,在修订版 200134中。但那是基于 C++1y 草案的该类型的早期化身。事实上,它是当时调用的定时共享互斥锁 std::shared_mutex

在最终的 C++14 标准发布std::shared_mutex之前重命名为std::shared_timed_mutex,因此在 GCC 5.1 版本(这是 5.x 系列中的第一个版本)之前,libstdc++ 中的类型被重命名,请参见修订版 207964

因此,尽管在 GCC 5.x 预发布阶段的某个时间点有一种std::shared_mutex类型,但它不是 C++17 未计时的类型,它在出现在任何 GCC 正式版本中之前就被重命名了。

然后,在 GCC 6.x 发布系列的开发过程中,添加了 C++1z 不定时共享互斥锁,重用了std::shared_mutex名称。这是上面评论中链接到的提交 TC,修订版 224158

所以 C++17 untimed shared_mutex从未出现在任何 GCC 5.x 版本中。在第一个 5.x 版本之前的一小段时间里,有一个名为的定时std::shared_mutex版本,但在所有适当的 5.x 版本中,它都被称为std::shared_timed_mutex.

第一个发布 C++17 不定时版本的版本是 2016 年 4 月的 6.1,因此您可以使用之后的任何 GCC 版本std::shared_mutex(只要您在编译器中启用 C++17,例如使用-std=gnu++17or-std=c++17标志)。

GCC 5 于 2015 年发布,因此期望能够在该版本中使用 C++17 有点不切实际。GCC 6.x 和 7.x 有很好的 C++1z 支持(当然,只是基于发布时的当前草案)。

于 2017-02-17T15:54:11.800 回答
0

按照此链接安装/升级到最新版本的 GCC 和 G++。http://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu

我已经在我的 ubuntu 上尝试过并且成功了。

于 2019-05-27T15:47:23.323 回答