std :: timed_mutex :: try_lock * fail spuriously [英] std::timed_mutex::try_lock* fail spuriously

查看:532
本文介绍了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 return false even if the mutex was not locked by any other thread at some point during timeout_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:


  1. 正如Boehm,Adve的Foundations of the C ++ Concurrency Memory ModelPLDI 08中指出的那样,强制执行数据无竞争的顺序一致性没有伪造的try_lock()失败的程序需要对try_lock()兼容互斥类型上的lock()操作明显更强的内存排序。在一些架构上,显着增加了无竞争互斥获取的成本。这个成本似乎大大超过禁止伪造try_lock()失败的任何好处。

  2. 它允许用户编写的try_lock()失败,例如,如果实现无法获取低 - 级锁用于保护互斥数据结构。

  3. 它确保客户端代码保持正确,例如,当调试线程被引入时,偶尔获取锁定以便能够从正在检查或检查的数据结构中读取一致的值。从try_lock()失败中获取信息的任何代码都会因引入另一个纯锁和读取数据结构的线程而中断。


这篇关于std :: timed_mutex :: try_lock * fail spuriously的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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