多久是ManualResetEventSlim一个旋 [英] How long is a single spin in ManualResetEventSlim
问题描述
多久是在C#中的单个自旋?我想知道的是有一个具有spinCount参数ManualResetEventSlim,我想知道每个自旋的单位是毫秒多久,或者它是如何工作的?我知道纺短的等待不是等待内核更高效。所以,我只是想看看我应该将此值设置为一份工作,一般需要2-10秒。
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.
推荐答案
有 spinCount
在构造函数中的参数和花在自旋等待的毫秒数之间没有相关性。
There is no correlation between spinCount
parameter in the constructor and the number of milliseconds spent doing a spin wait.
下面它是如何工作的。 MRES使用该 spinCount
参数要经过 Thread.SpinWait
的自己的日常等待独立。
Here is how it works. MRES uses this spinCount
parameter to go through its own waiting routine independent of Thread.SpinWait
.
- 调用之间的第10次迭代交替
Thread.Yield
和Thread.SpinWait
。到Thread.SpinWait
呼叫与自旋启动Environment.ProcessorCount * 4
,然后大约增加一倍对每个连续通话 - 此后每次迭代由20整除调用
的Thread.Sleep(1)
。 - 否则,那些被整除5个呼叫
的Thread.Sleep(0)
。 - 否则
Thread.Yield
被调用。 - 的
的CancellationToken
为100后每10次迭代检查。
- The first 10 iterations alternate between calling
Thread.Yield
andThread.SpinWait
. The call toThread.SpinWait
starts with a spin ofEnvironment.ProcessorCount * 4
and then approximately doubles on each successive call. - Thereafter each iteration divisible by 20 calls
Thread.Sleep(1)
. - Otherwise those divisible by 5 call
Thread.Sleep(0)
. - Otherwise
Thread.Yield
is called. - The
CancellationToken
is checked every 10 iterations after 100.
因此,大家可以看到有一个相当复杂的歌曲和舞蹈MRES的定制常规纺里想的。该算法可以从一个版本更改为版本。实在是没有办法预测每个自旋会持续多久。
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.
如果您的典型的等待时间2-10秒,然后你的代码几乎可以肯定是要做一个内核通过 Monitor.Wait
和 Monitor.Pulse
配位级等待,因为 spinCount
参数被限制在2047无妨。
If your typical wait times are 2-10 seconds then your code is almost certainly going to do a kernel level wait via Monitor.Wait
and Monitor.Pulse
coordinations because the spinCount
parameter is limited to 2047 anyway.
另外,2-10秒的时间很长。你真的想旋转那么久吗?
Plus, 2-10 seconds is a long time. Do you really want to spin that long?
这篇关于多久是ManualResetEventSlim一个旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!