C# 中的可升级读卡器锁
我有一个在多个线程之间共享的字典。每个线程根据给定的键从字典中读取特定值,但是 - 如果字典中不存在该键,则线程需要将其添加到字典中。
为了解决同步问题,我想使用 ReaderWriterLockSlim 类,它基本上为我提供了读取器-写入器锁同步(意味着读取器可以并行运行,但一次只能运行一个写入器......),但为读取器添加了升级选项。使用升级选项,我可以测试给定的密钥是否已在字典中,如果没有,则升级锁并写入它,承诺每个密钥只添加一个。
我的问题是我无法一次创建两个可升级锁 - 这意味着这个解决方案不好... :(
有人可以向我解释一下为什么微软选择以这种方式实现可升级锁(我不能有多个可升级锁)一次锁定...),并告诉我如何自己实现可升级的锁\给我另一个想法来同步我的共享字典?
I have a dictionary that is shared among number of threads. Every thread reads specific value from the dictionary according to a given key, but - if the key does not exist in the dictionary the thread need to add it to the dictionary.
To solve the sync problem, I though to use ReaderWriterLockSlim class which basically gives me readers-writers lock synchronization (meaning readers can run in parallel but only one writer at a time...) but adds upgrade option for a reader. Using the upgrade option I can test whether a given key is already in the dictionary and if not - upgrade the lock and write it, promising only one addition for every key.
My problem is that I cant create two upgradeable locks at a time - meaning this solution is no good... :(
Can somebody please explain to me why Microsoft chose to implement the upgradable lock this way (that I cant have more than one upgradable lock at a time...), and give me any idea how can I implement the upgradable lock by myself \ give me another idea to sync my shared dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 .NET 4.0 为什么不使用 ConcurrentDictionary
If you are using .NET 4.0 why not use the ConcurrentDictionary
我不知道为什么
ReaderWriterLockSlim
是以这种方式实现的。我怀疑有充分的理由。为什么不直接使用ConcurrentDictionary?那么你就不必担心显式锁定。
也就是说,我不认为拥有多个可升级读卡器锁会对您有什么帮助。考虑以下场景:
为了防止 Thread1 覆盖 Thread2 所做的事情,您必须编写以下逻辑:
当没有可升级锁时,这正是您必须执行的操作。
I have no idea why
ReaderWriterLockSlim
was implemented in that way. I suspect there are good reasons.Why not just use ConcurrentDictionary? Then you don't have to worry about explicit locking.
That said, I don't see where having multiple upgradable reader locks would help you. Consider the following scenario:
In order to prevent Thread1 from overwriting what Thread2 did, you'd have to write this logic:
Which is exactly what you have to do when there are no upgradeable locks.
UpgradeableReadLock 的存在使得您不必在获取写锁之前释放读锁。它允许您执行此操作:
而不是因为
它是 ReaderWriter 锁,您在任何给定时间仍然可能只有一个写入者,这是可升级的读锁强制执行的。这意味着这两者并不等同;第一个允许其他调用者潜入,但允许读取部分并发。
如果您可以使用 ReadLocks 进行大多数读取,并使用 UpgradeableRead 进行数据更新/插入,那么这就是目的。如果您的所有数据访问都是潜在的写入者,那么这可能不起作用,您可以在写入周围使用简单的锁(对象)来在添加/更新时强制执行独占访问。
UpgradeableReadLock exists so you do not have to release your read lock prior to acquiring a write lock. It lets you do this:
instead of
As it is a ReaderWriter lock, you may still only have one writer at any given time, which an upgradeable read lock enforces. This means these two aren't equivalent; the first lets other callers sneak in, but allows concurrency in the read portion.
If you have cases where you can use ReadLocks for most reads, and UpgradeableRead for data updates/inserts, this is the intent. If all of your data accesses are potential writers, then this probably doesn't work as well and you can use simple lock(object) around your writes to enforce the exclusive access when adding/updating.
听起来您可能没有使用此问题的最佳解决方案。
示例:
如果您使用的是 .NET 4,请尝试使用其他人提到的 ConcurrentDictionary 类。
It doesn't sound like you are perhaps using the optimal solution to this problem.
Example:
If you're using .NET 4, try using the ConcurrentDictionary class that others have mentioned.