简体   繁体   English

Threading.SpinWait 结构与 Thread.SpinWait 方法

[英]Threading.SpinWait struct vs Thread.SpinWait method

I was wondering if there was any difference between the Threading.Thread.SpinWait method and the Threading.SpinWait struct.我想知道Threading.Thread.SpinWait方法和Threading.SpinWait结构之间是否有任何区别。

In particular, what would be the idiomatic way to implement a spinwait in an application :特别是,在应用程序中实现自旋等待的惯用方法是什么:

Thread.SpinWait(1);

or或者

SpinWait spinWait = new SpinWait();
// ...
spinWait.SpinOnce();

Turns out the documentation of the SpinWait struct recommends not using the Thread.SpinWait() method directly.原来 SpinWait 结构的文档建议不要直接使用Thread.SpinWait()方法。 The correct way is :正确的方法是:

SpinWait spinWait = new SpinWait();
// ...
spinWait.SpinOnce();

SpinWait encapsulates common spinning logic. SpinWait 封装了常见的旋转逻辑。 On single-processor machines, yields are always used instead of busy waits, and on computers with Intel processors employing Hyper-Threading technology, it helps to prevent hardware thread starvation.在单处理器机器上,总是使用产量而不是忙等待,在采用超线程技术的英特尔处理器的计算机上,它有助于防止硬件线程饥饿。 SpinWait encapsulates a good mixture of spinning and true yielding. SpinWait 封装了旋转和真实屈服的良好组合。

SpinWait is a value type, which means that low-level code can utilize SpinWait without fear of unnecessary allocation overheads. SpinWait 是一种值类型,这意味着低级代码可以使用 SpinWait 而不必担心不必要的分配开销。 SpinWait is not generally useful for ordinary applications. SpinWait 通常对普通应用程序没有用处。 In most cases, you should use the synchronization classes provided by the .NET Framework, such as Monitor.在大多数情况下,您应该使用 .NET Framework 提供的同步类,例如 Monitor。 For most purposes where spin waiting is required, however, the SpinWait type should be preferred over the Thread.SpinWait method.但是,对于需要自旋等待的大多数用途,SpinWait 类型应优先于 Thread.SpinWait 方法。

http://www.albahari.com/threading/part5.aspx#_SpinLock_and_SpinWait adds some details : http://www.albahari.com/threading/part5.aspx#_SpinLock_and_SpinWait添加了一些细节:

In its current implementation, SpinWait performs CPU-intensive spinning for 10 iterations before yielding.在当前的实现中,SpinWait 在产生之前执行 10 次迭代的 CPU 密集型旋转。 However, it doesn't return to the caller immediately after each of those cycles: instead, it calls Thread.SpinWait to spin via the CLR (and ultimately the operating system) for a set time period.但是,它不会在每个周期后立即返回给调用者:相反,它调用Thread.SpinWait以通过 CLR(最终是操作系统)在设定的时间段内自旋。 This time period is initially a few tens of nanoseconds, but doubles with each iteration until the 10 iterations are up.这个时间段最初是几十纳秒,但每次迭代都会加倍,直到 10 次迭代结束。 This ensures some predictability in the total time spent in the CPU-intensive spinning phase, which the CLR and operating system can tune according to conditions.这确保在 CPU 密集型旋转阶段花费的总时间具有一定的可预测性,CLR 和操作系统可以根据条件进行调整。 Typically, it's in the few-tens-of-microseconds region — small, but more than the cost of a context switch.通常,它在几十微秒范围内——很小,但超过上下文切换的成本。 On a single-core machine, SpinWait yields on every iteration.在单核机器上,SpinWait 每次迭代都会产生收益。 You can test whether SpinWait will yield on the next spin via the property NextSpinWillYield.您可以通过属性 NextSpinWillYield 测试 SpinWait 是否会在下一次旋转时屈服。 If a SpinWait remains in “spin-yielding” mode for long enough (maybe 20 cycles) it will periodically sleep for a few milliseconds to further save resources and help other threads progress.如果 SpinWait 在“自旋产生”模式下保持足够长的时间(可能是 20 个周期),它将定期休眠几毫秒,以进一步节省资源并帮助其他线程前进。

In short, as i understand it, Threading.Thread.SpinWait does the actual spinning, and Threading.SpinWait encapsulates it so that it works well in almost all cases.简而言之,据我所知, Threading.Thread.SpinWait执行实际的旋转,而Threading.SpinWait封装,因此它几乎在所有情况下都能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM