ManualResetEventSlim recommended wait time
Asked Answered
F

2

23

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?

Fermat answered 19/6, 2014 at 15:34 Comment(2)
For me, "very short" is pretty meaningless. My rule is that if I expect the event to be set most of the time (i.e. I expect no wait), then I'll use ManualResetEventSlim. Otherwise I'll use ManualResetEvent. If you're writing a performance sensitive application, you should test your code with both to determine if there's any difference. If you're not counting microseconds, then it's probably a waste of time to worry about which one is faster.Across
"Very short" in this context means tens- to hundreds-of microseconds. The slim version spin-waits with the expectation that the other thread will release the lock in less time than a couple of context switches would take.Fulcrum
E
13

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:

Eshelman answered 19/6, 2014 at 16:42 Comment(0)
G
0

The scale from CPU cache to RAM and to Kernel is approximately logarithmic. Say you can do 400 cycles using CPU cache or 20 CPU cycles using reading / writing RAM or 1 kernel operation. The numbers will depend on hardware, it's just an estimate.

If you think that maximum wait times will be less than 6-15 ns (~20-50 CPU cycles @ 3GHz) than it's a small wait time.

Ginny answered 19/6, 2014 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.