c++11 线程的 RW 锁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16774469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:38:27  来源:igfitidea点击:

A RW lock for c++11 threads

c++multithreadingc++11

提问by gbjbaanb

I'd like to use the new standard threads instead of boost:threads but I've noticed the old shared_mutex is not available. What would be a good recommendation to replace this functionality and give me a multiple-readers, single-writer lock?

我想使用新的标准线程而不是 boost:threads 但我注意到旧的 shared_mutex 不可用。替换此功能并为我提供多读取器单写入器锁的好建议是什么?

回答by Andy Prowl

std::shared_mutexwill be part of the C++14 Standard Library. It did not make it to C++11 just because there was no time to formulate a proposal and discuss it thoroughly.

std::shared_mutex将成为 C++14 标准库的一部分。它没有进入 C++11 只是因为没有时间制定提案并对其进行彻底讨论。

You can still use boost::shared_mutexthough. Under Windows, if you are working with Windows Vista or later, you can use Slim Read-Write Locks, which are optimized for speed and memory consumption.

你仍然可以使用boost::shared_mutex。在 Windows 下,如果您使用的是 Windows Vista 或更高版本,则可以使用Slim Read-Write Locks,它针对速度和内存消耗进行了优化。

回答by Wandering Logic

You should take a look at the stack overflow question "C++11 equivalent to boost shared_mutex", and in particular the following linked email conversation: http://permalink.gmane.org/gmane.comp.lib.boost.devel/211180(which explains the resistance of the C++11 committee to approving shared_mutex). Also the following experiment on Joe Duffy's weblog: http://www.bluebytesoftware.com/blog/2009/02/12/ReaderwriterLocksAndTheirLackOfApplicabilityToFinegrainedSynchronization.aspx.

您应该查看堆栈溢出问题“ C++11 相当于 boost shared_mutex”,特别是以下链接的电子邮件对话:http: //permalink.gmane.org/gmane.comp.lib.boost.devel/ 211180(这解释了 C++11 委员会对批准 shared_mutex 的抵制)。还有乔达菲的博客上的以下实验:http: //www.bluebytesoftware.com/blog/2009/02/12/ReaderwriterLocksAndTheirLackOfApplicabilityToFinegrainedSynchronization.aspx

Every time you are considering a reader/writer lock, ask yourself the following 6 questions. If you can answer "no" to any of them then reader/writer locks are going to make your program worse, not better.

每次您考虑使用读/写锁时,问自己以下 6 个问题。如果您可以对其中任何一个回答“否”,那么读/写锁将使您的程序变得更糟,而不是更好。

  1. Is my shared object const? I have seen more incorrect uses of shared_mutexin my life than correct uses. To use a shared_mutexcorrectly it mustbe the case that you can declare your shared objects constinside the reader critical section without any compiler complaints. A "consumer" is notequivalent to "someone who does not mutate the data structure at all."
  2. Are my critical sections really long? Locking a shared_mutex is muchmore expensive than locking a regular mutex. You have to have a lotof work in your critical section to make up for the increased overhead of the lock acquire/release.
  3. Shouldmy critical sections be that long? You should ask yourself whether you really need to be doing all that work in a critical section. Often there is a bunch of preparatory work and/or work to massage the return object surrounding the constcalls to the shared object. Much of that extra work that isn't on the data-dependence path from the first use of the shared object to the last use of the shared object can be moved outside the critical section.
  4. Is lock contention really my performance problem? Even if your critical sections are long, you should be absolutely sure that lock contention is really your performance problem. If you aren't experiencing significant lock contention then switching to reader/writer locks isn't going to buy you anything.
  5. Could I reduce my lock contention by switching to a finer-grain locking scheme? Are you using a single lock to protect multiple objects? Can you give each object its own lock?
  6. Is the ratio of readers to writers significantlygreater than 1:1? Even if your critical sections are long and lock contention is a serious problem the ratio of readers to writers needs to be extremely high to get any benefit from reader/writer locks. The amount depends on costs for atomic instructions on your hardware and the quality of the particular implementations. (Joe Duffy finds that on his machine he needed a ratio around 20:1 readers:writers to make reader/writer locks a win.)
  1. 是我的共享对象const吗?我shared_mutex在生活中看到的不正确用法比正确用法更多。要shared_mutex正确使用 a ,您必须可以const在阅读器临界区中声明您的共享对象而没有任何编译器投诉。“消费者”等同于“根本不改变数据结构的人”。
  2. 我的临界区真的很长吗?锁定 shared_mutex比锁定常规互斥锁昂贵得多。你必须在你的临界区做很多工作来弥补锁获取/释放增加的开销。
  3. 如果我的关键部分是多长时间?您应该问问自己是否真的需要在关键部分完成所有工作。通常有一堆准备工作和/或工作来处理围绕const对共享对象的调用的返回对象。从第一次使用共享对象到最后一次使用共享对象的数据依赖路径上的大部分额外工作都可以移到临界区之外。
  4. 锁争用真的是我的性能问题吗?即使您的临界区很长,您也应该绝对确定锁争用确实是您的性能问题。如果您没有遇到严重的锁争用,那么切换到读取器/写入器锁不会给您带来任何好处。
  5. 我可以通过切换到更细粒度的锁定方案来减少我的锁争用吗?您是否使用单个锁来保护多个对象?你能给每个对象自己的锁吗?
  6. 读者与作者的比例是否明显大于 1:1?即使您的临界区很长并且锁争用是一个严重的问题,读取者与写入者的比率也需要非常高才能从读取者/写入者锁中获得任何好处。数量取决于硬件上原子指令的成本和特定实现的质量。(Joe Duffy 发现在他的机器上,他需要一个大约 20:1 的读者:作者的比例才能使读者/作者锁定获胜。)