多的读者,在升压单写锁 [英] Multiple-readers, single-writer locks in Boost
问题描述
我想实现在多线程的情况下code:
I'm trying to implement the following code in a multithreading scenario:
Get shared access to mutex
Read data structure
If necessary:
Get exclusive access to mutex
Update data structure
Release exclusive lock
Release shared lock
升压线程有一个 shared_mutex
类,它是专为多读者,单写模式。有关于这个类的几个计算器的问题。但是,我不知道它符合以上,其中的任何的读者可能会成为一个作家的情况。该文件指出:
Boost threads has a shared_mutex
class which was designed for a multiple-readers, single-writer model. There are several stackoverflow questions regarding this class. However, I'm not sure it fits the scenario above where any reader may become a writer. The documentation states:
该UpgradeLockable概念是一个
在SharedLockable细化
概念,允许升级
所有权以及共同所有权
独家拥有。这是一
延伸到所述多个读取器/
通过提供单写模式
SharedLockable理念:单
线程可能有升级的所有权
与此同时,正如其他人共享
所有权。
The UpgradeLockable concept is a refinement of the SharedLockable concept that allows for upgradable ownership as well as shared ownership and exclusive ownership. This is an extension to the multiple-reader / single-write model provided by the SharedLockable concept: a single thread may have upgradable ownership at the same time as others have shared ownership.
从单词单曲我怀疑只有一个线程可持有可升级锁。其他人只持有一个共享锁,不能的升级为独占锁。
From the word "single" I suspect that only one thread may hold an upgradable lock. The others only hold a shared lock which can't be upgraded to an exclusive lock.
你知道,如果的boost :: shared_lock
是在这种情况下非常有用(任何读者可能会成为一个作家),或者如果有任何其他的方式来实现这一目标?
Do you know if boost::shared_lock
is useful in this situation (any reader may become a writer), or if there's any other way to achieve this?
推荐答案
的boost :: shared_lock
在这种情况下不利于(多的读者,才能成为作家)因为只有一个线程可能拥有可升级锁。这是由双方在这个问题的文档报价暗示,并通过查看code(螺纹\\ WIN32 \\ shared_mutex.hpp)。如果一个线程尝试,而另一个线程各执一份获得一个升级锁,它会等待其他线程。
boost::shared_lock
doesn't help in this situation (multiple readers that can become writers), since only a single thread may own an upgradable lock. This is both implied by the quote from the documentation in the question, and by looking at the code (thread\win32\shared_mutex.hpp). If a thread tries to acquire an upgradable lock while another thread holds one, it will wait for the other thread.
我自上关键部分是短期使用所有读/写器定期锁,这是在我的情况好解决。
I settled on using a regular lock for all reader/writers, which is OK in my case since the critical section is short.
这篇关于多的读者,在升压单写锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!