[英]Is ManualResetEventSlim with spinCount = 0 the same as ManualResetEvent?
As far as I know, slim versions of synchronization primitives use spin waits before using resources of the kernel.据我所知,精简版的同步原语在使用内核资源之前使用自旋等待。
Is it true that ManualResetEventSlim
with spinCount
= 0 is the same as the usual ManualResetEvent
? spinCount
= 0 的ManualResetEventSlim
是否与通常的ManualResetEvent
相同?
new ManualResetEventSlim(initialState: false, spinCount: 0);
If no, what is the essential difference between them?如果不是,它们之间的本质区别是什么? Do we have any benefits of using the slim-version without spin waits?
使用没有自旋等待的 slim-version 对我们有什么好处吗?
Looking at the source for Wait if you set the spin count to 0 then it's basically a ManualResetEvent
but instead of using a kernel wait event it actually uses a monitor.查看Wait的源代码,如果将自旋计数设置为 0,那么它基本上是一个
ManualResetEvent
,但它实际上使用的不是内核等待事件,而是监视器。
The kernel wait event is only allocated if you explicitly ask for it, which save a transition into kernel mode to allocate it and set/reset it.内核等待事件仅在您明确要求时才会分配,这会保存到内核模式的转换以分配它并设置/重置它。
It's worth considering that although the ManualResetEventSlim
won't spin if you set the spin count to 0 it's highly likely that the call to Monitor.Wait
will spin, so I suspect you're not actually avoiding a spin.值得考虑的是,如果您将旋转计数设置为 0,尽管
ManualResetEventSlim
不会旋转,但对Monitor.Wait
的调用很可能会旋转,所以我怀疑您实际上并没有避免旋转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.