使用 ReaderWriterLock 的真正缺点是什么
我们有一个针对.NET 2.0 RTM 的项目(是的,它应该是.NET 2.0 RTM,我们有一些正统的客户)。我只是想知道 ReaderWriterLock?为什么它如此糟糕以至于每个人都告诉“不要使用它,尝试使用其他东西,比如 lock
语句”?如果我们可以使用.NET 3.5,我肯定会使用 ReaderWriterLockSlim,但是有了 ReaderWriterLock
,我对来自四面八方的所有这些警告感到有点害怕。有人测量性能或其他什么吗?如果存在一些性能问题,在什么负载下我们会遇到这些问题?
就 ReaderWriterLock 的主要用途而言,我们有一个典型的情况,即多次读取而很少写入。使用 lock
语句会阻止所有读取器。也许这对我们来说不是一个可怕的问题,但如果我可以使用 ReaderWriterLock 我会更满意。在我看来,引入多个显示器确实是一个非常非常糟糕的主意。
We have project targeted .NET 2.0 RTM (yes, it should be .NET 2.0 RTM, we have some orthodox clients). And I'm just wondering what are the downsides of ReaderWriterLock? Why is it so bad that everyone tell "don't use it, try to use something else like lock
statement"? If we could use .NET 3.5, I would definitely use ReaderWriterLockSlim, but with ReaderWriterLock
I'm a little scary with all these warning coming from everywhere. Does anybody measured performance or whatever? If there are some performance issues, under what payload can we encounter them?
We have a classic situation in terms of ReaderWriterLock
main purpose, i.e. multiple reads and rarely writes. Using lock
statement would block all readers. Perhaps it's not an awful issue for us, but if I could use ReaderWriterLock
I would be more satisfied. IMO introducing several monitors is a very very bad idea indeed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下几篇文章可以为您提供您正在寻找的想法。
性能ReaderWriterLockSlim 与 ReaderWriterLock 的比较
Rico Mariani 使用ReaderWriterLock 部分 这篇文章解释了使用 ReaderWriterLock 的一些成本和场景,另请查看 第 1 部分 和 第 2 部分
Jeffrey Richter 探讨 ReaderWriterLock 的替代方案
Following few posts can provide you the ideas you are looking for.
Performance Comparison of ReaderWriterLockSlim with ReaderWriterLock
Rico Mariani on Using ReaderWriterLock part this post explains some of the costs and scenarios to use ReaderWriterLock, also check part 1 and part 2
Jeffrey Richter on an alternative to ReaderWriterLock
如果您确实面临 ReaderWriterLock 的理想用例(即许多并发读取和少量写入),那么请使用它!
如果您发现它太慢,还有其他选择。 Sanjeevakumar 在他的回答中提供了一些链接。我也会提供一份:
低锁技术实际应用:实现读写锁
我将在这里引用该链接的要点:
If you are really facing the ideal use case for ReaderWriterLock (i.e. many concurrent reads and few writes) then use it!
If you find it is too slow there are alternatives. Sanjeevakumar provided some links in his answer. I'll provide one in mine too:
Low-Lock Techniques in action: Implementing a Reader-Writer lock
I'll quote the takeaways from that link here: