.NET 中的跨进程读写同步原语?
是否有跨进程工作的读/写锁定机制(类似于互斥锁,但读/写而不是独占锁定)?我想允许并发读取访问,但允许独占写入访问。
Is there a read/write locking mechanism that works across processes (similar to Mutex, but read/write instead exclusive locking)? I would like to allow concurrent read access, but exclusive write access.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Windows 不包含跨进程读写锁。可以使用信号量和互斥量的组合来构造互斥量(互斥量由写入者持有以进行独占访问,或者由读取者持有,然后读取者使用信号量释放其他读取者,即写入者将仅等待互斥体,而读取者则等待) 。
但是,如果预期争用较低(即没有线程长时间持有锁),则互斥可能仍然会更快:读写器锁的额外复杂性压倒了允许多个读取器进入的任何好处。(读写器锁)只有当有更多的读取器并且锁被持有很长一段时间时,锁才会更快,但只有您的分析才能证实这一点。)
Windows does not include a cross process Reader-Writer lock. A combination of Semaphore and Mutex could be used to construct ones (the Mutex is held by a writer for exclusive access or by a Reader which then uses the Semaphore to release other readers—i.e. writers would wait on just the mutex and readers for either).
However, if contention is expected to be low (i.e. no thread holds a lock for long) then mutual exclusion may still be faster: the additional complexity of the reader-writer lock overwhelms any benefit of allowing multiple readers in. (A reader-writer lock will only be faster if there are many more readers and locks are held for significant time—but only your profiling can confirm this.)
不会。正如 Richard 上面指出的,.NET 中不存在这种开箱即用的机制。
这是使用互斥锁和信号量来实现它的方法。
方法 #1 在 http://www.joe Cheng.com/blog/entries 中描述/Writinganinter-processRea.html,引用:
另一种选择是:
读锁定 - 如上所述。写锁定如下(伪代码):
必须注意的是,可以采用更有效的方法,如下所示: http://en.wikipedia.org/wiki/Readers-writers_problem#The_second_readers-writers_problem
在上面的文章中查找“此解决方案不是最优的”字样。
No. As Richard noted above, there is no such out of the box mechanism in .NET.
This is how to implement it using a mutex and a semaphore.
Method #1 is described in http://www.joecheng.com/blog/entries/Writinganinter-processRea.html, quoting:
An alternative would be:
Read locking - as above. Write locking as follows (pseudocode):
It must be noted that more efficient approach is possible, as here: http://en.wikipedia.org/wiki/Readers-writers_problem#The_second_readers-writers_problem
Look for the words "This solution is suboptimal" in the article above.
我根据帕维尔的回答创建了这个课程。我还没有对其进行广泛的测试,但我已经创建了一个简单的 winforms 应用程序来测试它,到目前为止它运行良好。
请注意,它使用信号量,因此不支持重入。
I've created this class based on Pavel's answer. I haven't tested it extensively yet, but I've created a simple winforms application to test it and so far it works well.
Please note, that it uses a semaphore, so it doesn't support reentrancy.
如果您想避免作家饥饿,那么您可以考虑另一种算法。我研究了一些算法,这些算法避免了 Writer 问题的饥饿(例如在这篇论文中)。解决方案建议伪代码之一如下:伪代码图像。
不幸的是,ctr 变量是一个整数,因此它只能在进程间场景中工作。我决定用信号量计数器 (
ReaderCounter
) 替换整数计数器,以便它可以用于跨进程通信。本质上,我使用WaitOne(0)
来减少,使用Release()
来增加读取器计数器。注:为了更方便使用,读取器计数器中添加了 1 个吹气计数。例如,使用 5 读取器意味着 [1,6] 初始信号量计数。从最小计数减少返回 -1,从最大计数增加返回最大计数 +1。
更新:我已经使用控制台应用程序创建了一个 GitHub 存储库,因此您可以使用它。它还包含具有
TryEnterReadLock()
和TryEnterWriteLock()
方法的 ReaderWriterSynchronizer:https://github.com/SzilvasiPeter/Cross-process-ReaderWriterLockIf you want to avoid Writer starvation then you could consider another algorithm. I research some algorithms, which avoid the starvation of the Writer problem (e.g. in this paper). One of the solution proposal pseudo-code is the following: pseudo-code image.
Unfortunately,
ctr
variable is an integer, therefore it could only work in interprocess scenarios. I decided to replace the integer counter with a Semaphore counter (ReaderCounter
) so it could be used for cross-process communication. Essentially, I usedWaitOne(0)
in order to decrease andRelease()
to increase the reader counter.NOTE: For easier usage, 1 puffer count was added to the reader counter. For example, using a 5 reader means [1,6] initial Semaphore count. Decreasing from the minimum count returns with -1 and Increasing from maximum count return with maximum count +1.
UPDATE: I have created a GitHub repository with console applications, so you can play with it. It also contains ReaderWriterSynchronizer with
TryEnterReadLock()
andTryEnterWriteLock()
methods: https://github.com/SzilvasiPeter/Cross-process-ReaderWriterLockSystem.Threading.Mutex 有一个互斥体,可用于进程内通信。如果您想要它不支持的功能,可以通过互斥体来实现。
System.Threading.Mutex has a mutex that can be used for intra-process communication. If you would like functionality that it doesn't support, it can be implemented via a mutex.
您看过 System.Threading.ReaderWriteLock 吗?这是 MSDN 链接。
Have you looked at
System.Threading.ReaderWriteLock
? Here's the MSDN Link.