std :: timed_mutex :: try_lock * fail spuriously [英] std::timed_mutex::try_lock* fail spuriously
问题描述
通过 try_lock *
,我的意思是 try_lock()
, try_lock_for
和 try_lock_until()
。根据 cppreference ,所有三种方法可能只是失败地失败。以下内容来自 try_lock_for()
By try_lock*
, I take to mean try_lock()
, try_lock_for()
, and try_lock_until()
. According to cppreference, all three methods may just fail spuriously. Following is quoted from the description for try_lock_for()
$ c> try_lock(),此函数允许错误失败,
返回false
,即使互斥锁未被任何锁其他线程在
的某一点在timeout_duration
。
As with
try_lock()
, this function is allowed to fail spuriously and returnfalse
even if the mutex was not locked by any other thread at some point duringtimeout_duration
.
那么虚假唤醒可能发生在 std :: condition_variable
及其背后的原理。但是,mutex是什么情况?
I know that spurious wakeup may happen with std::condition_variable
and the rationale behind it. But, what is the case with a mutex?
推荐答案
根据: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3209.htm
另一方面,有强烈的理由要求程序被写为容忍伪造的try_lock p>
On the other hand, there are strong reasons to require that programs be written to tolerate spurious try_lock() failures:
- 正如Boehm,Adve的Foundations of the C ++ Concurrency Memory ModelPLDI 08中指出的那样,强制执行数据无竞争的顺序一致性没有伪造的try_lock()失败的程序需要对try_lock()兼容互斥类型上的lock()操作明显更强的内存排序。在一些架构上,显着增加了无竞争互斥获取的成本。这个成本似乎大大超过禁止伪造try_lock()失败的任何好处。
- 它允许用户编写的try_lock()失败,例如,如果实现无法获取低 - 级锁用于保护互斥数据结构。
- 它确保客户端代码保持正确,例如,当调试线程被引入时,偶尔获取锁定以便能够从正在检查或检查的数据结构中读取一致的值。从try_lock()失败中获取信息的任何代码都会因引入另一个纯锁和读取数据结构的线程而中断。
这篇关于std :: timed_mutex :: try_lock * fail spuriously的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!