.Net 中哪些场景构成非默认执行上下文?
WaitHandle.WaitOne() 有一个 ExitContext 选项,允许在保留另一个资源锁之前临时释放资源锁。这在某些可能发生死锁或线程匮乏的情况下很有用。
msdn 文档讨论了 dondefault 上下文。它们仅引用构成涉及 ContexBoundObject 的非默认上下文的示例,请参阅 (相关问题)。
其他同步选项(例如 Monitor.Enter()、Lock{} 语句)是否也构成非默认上下文?还有哪些其他场景会将线程执行置于非默认上下文中?
WaitHandle.WaitOne() has an ExitContext option to allow for temporary release of a resource lock before holding for another. This is useful in some cases where dead-lock or thread starvation may occur.
The msdn documentaiton talks about a dondefault context. They only refer to examples which constitutes being in a nondefault context involving ContexBoundObject see (Related Qusetion).
Do other synchronization options such as Monitor.Enter(), Lock{} statements also constitute nondefault context? What other scenarios would place a thread execution in nondefault context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的话题,我对此了解不够。我所知道的是它与远程处理场景相关。通过传递 True,您可以避免通信阻塞,直到解决等待并允许调度其他消息。是的,可以避免死锁,但可能会导致同步问题。
WaitOne() 的这个参数引起了很多混乱和 FUD,以至于 .NET 2.0 SP1 进行了兼容性破坏性更改。他们添加了 WaitOne(int) 和 WaitOne(TimeSpan) 重载,以避免猜测 exitContext 参数的正确值。通常应该是 False。
It is a tricky subject and I don't know enough about it. What I do know is that it is relevant in Remoting scenarios. By passing True, you can avoid communication from blocking until the wait is resolved and allow other messages to be dispatched. Yes, avoids deadlock but can cause synchronization problems.
This argument to WaitOne() has caused so much confusion and FUD that .NET 2.0 SP1 had a compatibility breaking change. They added the WaitOne(int) and WaitOne(TimeSpan) overloads to avoid having to guess at the proper value of the exitContext argument. Which should normally be False.