简体   繁体   English

如何在标准C ++ 11中创建shared_lock或upgrade_lock?

[英]How to make a shared_lock or upgrade_lock in standard C++11?

I am missing very much from the new standard the std::shared_lock template class. 我从新标准std::shared_lock模板类中遗漏了很多东西。 In Boost.Thread there is boost::shared_lock , even boost::upgrade_lock exists. 在Boost.Thread中有boost::shared_lock ,甚至还存在boost::upgrade_lock

Why is that, there is no std::shared_lock and std::unique_lock in C++11? 为什么这样,C ++ 11中没有std :: shared_lock和std::unique_lock
How is it possible to acquire a similar behavior as boost::shared_lock has, but in pure C++11? 如何获得类似boost::shared_lock的类似行为,但在纯C ++ 11中呢?

I was thinking to use boost::shared_lock<std::mutex> , but this doesn't have so much sense, since std::mutex does not have a lock_shared() member. 我正在考虑使用boost::shared_lock<std::mutex> ,但这没有多大意义,因为std::mutex没有lock_shared()成员。 And also, there is no such as std::shared_mutex . 而且,没有像std::shared_mutex

Howard's proposal for std::shared_mutex was turned down for C++11 due to lack of time to consider it properly. 霍华德对std::shared_mutex的提议由于没有时间正确考虑而被C ++ 11拒之门外。 He's proposed it again for C++17, and it's being discussed at the meeting in Portland this week. 他再次为C ++ 17提出了这个问题,本周在波特兰举行的会议上对此进行了讨论。

In the mean time, if you can use Boost then you may as well; 同时,如果你可以使用Boost那么你也可以; there won't be any new functionality for it being standardized. 标准化不会有任何新功能。

That said, it's worth checking that using shared_mutex is actually of benefit --- in many cases it doesn't provide the hoped-for performance gains due to contention on the mutex itself. 也就是说,值得检查的是,使用shared_mutex实际上是有益的 - 在许多情况下,由于互斥锁本身的争用,它无法提供所希望的性能提升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM