使用 ReaderWriterLock 的真正缺点是什么

发布于 2024-10-31 20:23:18 字数 712 浏览 1 评论 0原文

我们有一个针对.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2024-11-07 20:23:18

以下几篇文章可以为您提供您正在寻找的想法。

性能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

月亮邮递员 2024-11-07 20:23:18

如果您确实面临 ReaderWriterLock 的理想用例(即许多并发读取和少量写入),那么请使用它!

如果您发现它太慢,还有其他选择。 Sanjeevakumar 在他的回答中提供了一些链接。我也会提供一份:
低锁技术实际应用:实现读写锁

我将在这里引用该链接的要点:

  1. 按照我第一篇文章中的建议使用读写锁。如果锁定性能存在问题,请采用此处的实现作为 .NET System.ReaderWriterLock 的“直接”替代。在 Microsoft 修复其库中的版本之前,请随意使用此实现。
  2. 自旋锁是一种非常有价值的低锁技术。这里用来制作一个干净、简单但高效的读写锁,完全用托管代码编写。这个锁比我见过的大多数实现要简单得多,但也接近最佳。原因是自旋锁的使用极大地简化了锁的设计和分析,而不会牺牲性能。请随意使用此处显示的自旋锁实现来创建其他高性能并发构造。
  3. 即使是像读写锁这样简单的东西,其行为也有微妙之处(特别是当您考虑性能问题时)。保持简单在这里确实有回报。

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:

  1. Use reader-writer locks as suggested in my first article. If lock perf is a problem, take the implementation here as a 'drop in' replacement for the .NET System.ReaderWriterLock. Feel free to use this implementation until Microsoft fixes the version in its library.
  2. Spin-locks are a really valuable low lock technique. There were used here to make a clean, simple but efficient reader-writer lock written competely in managed code. This lock is significantly simpler than most implementations I have seen, yet is clost to optimal. The reason for this is that the use of spin locks GREATLY simplifies the design and analysis of the lock, without sacrificing performance. Feel free to use the Spin lock implementation show here to make other high performance concurrent constructs.
  3. Even something as simple as a Reader-Writer lock has subtleties about its behavior (especially when you factor in performance concerns). Keeping it simple really pays off here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文