饥饿与upgrade_lock [英] Starvation with upgrade_lock

查看:139
本文介绍了饥饿与upgrade_lock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Boost的 upgrade_lock (使用<一个href=\"http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write\">this例如的,但我碰上了饥饿问题。

I am trying to use Boost's upgrade_lock (using this example, but I run into a starvation issue.

我实际使用从code <一个href=\"http://stackoverflow.com/questions/12082405/boost-shared-lock-read-$p$pferred/13445989#13445989\">this帖子,但我想一个上最新的讨论。我400线后的 WorkerKiller 的运行。我遇到相同问题的的 anoneironaut 的,笔者mentionned门柱。

I am actually using the code from this post, but I wanted an up-to-date discussion. I run 400 threads after the WorkerKiller. I run into the exact same problem as anoneironaut, the author of the mentionned post.

我见过的命题从的霍华德Hinnant(欣南特)的,但我真的不希望有更多的外部code(而且我不能让他的编译截至目前),并发布注释以后6个月指出升压使用一个公平的实现,现在的(12月3日'12)。

I have seen the proposition from Howard Hinnant, but I don't really want to include more external code (moreover I cannot get his to compile as of now) and a comment posted 6 months later states that "Boost uses a fair implementation now" (Dec 3 '12).

借助提升1.55文档指出

Note the the lack of reader-writer priority policies in shared_mutex. This is 
due to an algorithm credited to Alexander Terekhov which lets the OS decide 
which thread is the next to get the lock without caring whether a unique lock or 
shared lock is being sought. This results in a complete lack of reader or writer
starvation. It is simply fair.". 

和记算法的亚历山大捷列霍夫一霍华德Hinnant(欣南特)挂在嘴边,所以我预计1.55推动实施的行为像霍华德Hinnant(欣南特)的回答,这情况并非如此。它的行为完全一样的问题。

And the algorithm credited to Alexander Terekhov is the one that Howard Hinnant talks about, so I would expect the 1.55 boost implementation to behave like in Howard Hinnant's answer, which is not the case. It behaves exactly like in the question.

为什么这样说我的 WorkerKiller 的饥饿遭罪?

Why is it the case that my WorkerKiller suffers of starvation?

更新:它与这个code 观察:


  • Debian的64位,提升1.55(包括Debian的版本,一个来自源编译),既铿锵++和g ++

  • Ubuntu的64位,提升1.54,既有铿锵++(3.4-1ubuntu1)和g ++(4.8.1-10ubuntu9)

推荐答案

这是一个微妙的。所不同的涉及升压共享和可升级的所有制及其实现的概念,和

This is a subtle one. The difference involves the concepts of shared and upgradable ownerships, and their implementations in Boost.

让我们首先得到共享所有权和所有权升级的概念整理出来。
对于<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts.shared_lockable\"相对=nofollow> SharedLockable ,线程必须事先决定是否要改变对象(需要独家拥有)或者从它只读(共享所有权就足够了)。如果与共享所有权线程决定它要改变对象时,它首先必须释放的对象其共享锁,然后建立一个新的,独占锁。在这两个步骤之间,所述螺纹保持在所有的对象上没有锁。试图构建从已经持有共享锁就会死锁线程独占锁,因为排他锁的构造函数将阻塞,直到所有共享锁已被释放。

Let's first get the concepts of shared ownership and upgradable ownership sorted out. For a SharedLockable, a thread must decide beforehand whether it wants to change the object (requiring exclusive ownership) or only read from it (shared ownership suffices). If a thread with shared ownership decides it wants to change the object, it first must release its shared lock on the object and then construct a new, exclusive lock. In between these two steps, the thread holds no locks at all on the object. Attempting to construct an exclusive lock from a thread that already holds a shared lock will deadlock, as the exclusive lock constructor will block until all shared locks have been released.

<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts.upgrade_lockable\"相对=nofollow> UpgradeLockable 克服了这一限制,允许不释放的它来升级共享锁定到独占锁定的。也就是说,线程不断互斥活性锁定在任何时候,从获得在此期间的排他锁,禁止其他线程。除此之外,UpgradeLockable仍然允许从SharedLockable所有操作,前者概念是后者的一个超集。在<一个href=\"http://stackoverflow.com/questions/12082405/boost-shared-lock-read-$p$pferred/13445989#13445989\">question您链接到是只关心SharedLockable概念。

UpgradeLockable overcomes this limitation, by allowing to upgrade a shared lock to an exclusive lock without releasing it. That is, the thread keeps an active lock on the mutex at all times, prohibiting other threads from obtaining an exclusive lock in the meantime. Besides that, UpgradeLockable still allows all operations from SharedLockable, the former concept is a superset of the latter. The question you linked to is only concerned with the SharedLockable concept.

无论是概念,通过升压规定,要求实施是公平的。但是,<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_types.shared_mutex\"相对=nofollow> shared_mutex ,这是Boost的最小实现了SharedLockable确实给你的问题​​所引述的公平性保证。请注意,这是一个的附加保障的什么概念实际上需要。

Neither concept, as specified by Boost, requires an implementation to be fair. However, the shared_mutex, which is Boost's minimal implementation for a SharedLockable does give the fairness guarantees quoted in your question. Note that this is an additional guarantee to what the concept actually requires.

不幸的是,最小的实施升级所有权,请查看 upgrade_mutex ,不给这个额外出示担保。它仍然实现了共享所有权概念作为升级所有权的要求,但由于不需要对符合标准的实现公平,他们不提供它。

Unfortunately, the minimal implementation for upgradable ownership, the upgrade_mutex, does not give this additional gurantee. It still implements the shared ownership concept as a requirement for upgradable ownership, but since fairness is not required for a conforming implementation, they do not provide it.

作为<一个href=\"http://stackoverflow.com/questions/21773618/starvation-with-upgrade-lock/21778949?noredirect=1#comment45138062_21778949\">pointed出霍华德在评论,捷列霍夫的算法可以平凡调整与升级的锁以及工作,它只是在加速实施目前不支持这一点。

As pointed out by Howard in the comments, Terekhov's algorithm can be trivially adjusted to work with upgradable locks as well, it's just that the Boost implementation does not support this currently.

这篇关于饥饿与upgrade_lock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆