How long is a single spin in c#? What I want to know is there is a ManualResetEventSlim that has a spinCount parameter and I want to know how long each spin is in milliseconds or how it works? I know spinning is more efficient for short waits than a kernel wait. So I am just trying to see what I should set this value to for a job that generally takes 2-10 sec.
Share
There is no correlation between
spinCountparameter in the constructor and the number of milliseconds spent doing a spin wait.Here is how it works. MRES uses this
spinCountparameter to go through its own waiting routine independent ofThread.SpinWait.Thread.YieldandThread.SpinWait. The call toThread.SpinWaitstarts with a spin ofEnvironment.ProcessorCount * 4and then approximately doubles on each successive call.Thread.Sleep(1).Thread.Sleep(0).Thread.Yieldis called.CancellationTokenis checked every 10 iterations after 100.So as you can see there is a fairly complex song-and-dance going on inside MRES’s custom spinning routine. And the algorithm could change from version to version. There is really no way to predict how long each spin will last.
If your typical wait times are 2-10 seconds then your code is almost certainly going to do a kernel level wait via
Monitor.WaitandMonitor.Pulsecoordinations because thespinCountparameter is limited to 2047 anyway.Plus, 2-10 seconds is a long time. Do you really want to spin that long?