c# - C# Monitor.Wait() 是否遭受虚假唤醒?

标签 c# java multithreading

Java 的 Object.wait()警告“虚假唤醒”,但 C# 的 Monitor.wait()好像根本没提。

看看Mono是如何在Linux之上实现的,Linux有spurious wakeups ,这不应该记录在某处吗?

最佳答案

乔·达菲的 "Concurrent Programming On Windows"提到了这一点(P311-312,P598)。这一点很有趣:

Note that in all of the above examples, threads must be resilient to something called spurious wake-ups - code that uses condition variables should remain correct and lively even in cases where it is awoken prematurely, that is, before the condition being sought has been established. This is not because the implementation will actually do such things (although some implementations on other platforms like Java and Pthreads are known to do so), nor because code will wake threads intentionally when it's unnecessary, but rather due to the fact that there is no guarantee around when a thread that has been awakened will become scheduled. Condition variables are not fair. It's possible - and even likely - that another thread will acquire the associated lock and make the condition false again before the awakened thread has a chance to reacquire the lock and return to the critical region.

然后他给出了正常的模式,用于一段时间循环测试条件。

我想说,从这里可以合理地预期 Monitor.Wait 不会 通常过早唤醒你,如果你绝对知道 em> 没有其他东西可以改变条件,那么您可能可以在没有条件循环的情况下逃脱:但是无论如何包含它会更安全,以防您的逻辑不准确。

关于c# - C# Monitor.Wait() 是否遭受虚假唤醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1461913/

相关文章:

c# - 如何在 BaseController 中初始化 BaseModel

c# - WPF如何绑定(bind)ComboBox

java - MPAndroidChart 插入 Y 轴 listArray 变量或动态参数

c# - 如何控制正在单个文件中写入的线程;不能使用锁

multithreading - 我可以暂时为线程启用 FTZ 和 DAZ 浮点模式吗?

c# - 自定义错误页面的重定向循环

java - 选择ListPreference时播放声音

Java 反射框架和安全性

c++ - 减少多线程体素算法中的互斥量开销

c# - C# 中 Oracle 依赖项的更改通知