22

The MSDN documentation for ManualResetEventSlim states

You can use this class for better performance than ManualResetEvent when wait times are expected to be very short.

How long is "very short"? At what point will the benefit of using a kernel object with a ManualResetEvent outweigh the overhead of instantiating it?

4

2 回答 2

12

This is what I found and would love for someone else to validate this, but this is what I found while reading the Reference Source for ManualResetEventSlim

ManualResetEventSlim
It is attempting to just sleep and yield based on the number of processors and doing extremely short sleeps -- 1ms or 0ms depending on its current spin index. If it still hasn't been enough time, it will then revert to using Monitor.Wait using a new updated version of the timeout that was originally passed in.

Passing in 0 for Thread.Sleep relieves its time slice.

ManualResetEvent
It uses WaitHandle and calls native methods to handle waiting for the specified time. Unfortunately, I am unable to see what it is doing.

My Conclusion
"Very Short" means just a few milliseconds.

EDIT: I just found these which have lots of information:

于 2014-06-19T16:42:51.483 回答
0

从 CPU 缓存到 RAM 再到内核的比例大约是对数。假设您可以使用 CPU 缓存执行 400 个周期或使用读/写 RAM 或 1 个内核操作执行 20 个 CPU 周期。这些数字将取决于硬件,这只是一个估计值。

如果您认为最大等待时间将小于 6-15 ns(约 20-50 个 CPU 周期 @ 3GHz),那么这是一个很小的等待时间。

于 2014-06-19T16:26:37.320 回答