gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:41:19 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com