的boost :: shared_mutex多读/单写互斥 [英] boost::shared_mutex multiple-reader / single-writer mutex
问题描述
我想使用boost :: shared_mutex实现多读/单写互斥。我的问题是相当简单的,有可能是一个线程来获得读者访问shared_mutex,当另一个线程试图锁定该shared_mutex写?例如,我有10个线程,只有一个可以写,
I am trying to use boost::shared_mutex to implement a multiple-reader / single-writer mutex. My question is fairly simple, is it possible for a thread to gain reader access to a shared_mutex, when another thread tries to lock that shared_mutex for writing? For example, I have 10 threads, only one of them can write,
- 线程1对是shared_mutex一个shared_lock并尝试读取
东西 - 2线设在该shared_mutex一个shared_lock并尝试读取
东西 - 3线设在该shared_mutex一个unique_lock并尝试写东西
- 螺纹4具有上shared_mutex一个shared_lock并尝试读取
东西 - 螺纹5对是shared_mutex一个shared_lock并尝试读取
东西
该shared_mutex目前赞同线程2 ,我的问题是,是否有可能的螺纹4 可以获得读访问该shared_mutex,之前的线程锁定3 能写?是否有可能为一个读/写互斥曾经进入一个饥饿的情况下,例如,100读者V.S. 1作家?
The shared_mutex is currently shared locked by thread 2, my question is whether it is possible that thread 4 can gain read access to that shared_mutex, before thread 3 can write? Is it possible for a reader/writer mutex ever gets into a starvation situation, e.g., 100 reader v.s. 1 writer?
感谢。
推荐答案
显然,的boost :: shared_mutex
离开的公平策略到执行。它可以是公平的,读者过作家或作家在阅读器等等,这取决于它是为您的特定版本,它有可能是作家,可以饿死。
Apparently the boost::shared_mutex
leaves the fairness policy up to the implementation. It can be either fair, reader-over-writer or writer-over-reader so depending on which it is for your particular version it's possible that the writer can be starved.
这篇关于的boost :: shared_mutex多读/单写互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!