【发布时间】:2014-03-13 11:24:20
【问题描述】:
我正在尝试使用 Boost 的 upgrade_lock(使用 this example,但我遇到了饥饿问题。
我实际上正在使用来自this post 的代码,但我想要一个最新的讨论。我在 WorkerKiller 之后运行了 400 个线程。我遇到了与上述帖子的作者 anoneironaut 完全相同的问题。
我已经看到了 Howard Hinnant 的提议,但我真的不想包含更多外部代码(而且我现在无法编译他的代码)和 6 个月后发表的评论声明“Boost 现在使用公平的实现”(2012 年 12 月 3 日)。
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.".
并且归功于 Alexander Terekhov 的算法是 Howard Hinnant 所谈论的算法,所以我希望 1.55 boost 实现的行为类似于 Howard Hinnant 的回答,即并非如此。它的行为与问题中的完全一样。
为什么我的WorkerKiller会挨饿?
更新:在this code 上观察到:
- Debian x64,Boost 1.55(Debian 版本和从源代码编译的版本),同时包含 clang++ 和 g++
- Ubuntu x64,Boost 1.54,clang++ (3.4-1ubuntu1) 和 g++ (4.8.1-10ubuntu9)
【问题讨论】:
-
您在什么编译器和平台上运行?我无法在 VC11 x64 构建上重现 Boost 1.55 的问题。
-
这很有趣。我复制了我的代码here。它在 Debian (boost 1.55) 和 Ubuntu (boost 1.54) 上使用 clang++ 和 g++ 进行了测试。
-
现在知道了,我没有意识到您使用的是
upgrade_lock而不是普通的独占锁。
标签: multithreading boost mutex