.net Thread.SpinWait 方法的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:58:47  来源:igfitidea点击:

What's the purpose of Thread.SpinWait method?

.netmultithreading

提问by Drake

From the MSDNis not really clear its purpose.

MSDN 上并没有真正明确它的用途。

Can it be used to simulate an intensive CPU calculation test?

可以用来模拟密集的CPU计算测试吗?

回答by gbjbaanb

no. Its used as a replacement for very short-term sleep calls.

不。它用作非常短期的睡眠呼叫的替代品。

When you do multi-threaded locking, if the resource you're attempting to acquire is already locked, you typically go to sleep and wait for it to become free. When you do this, you give up the rest of the time that you were allocated by the scheduler to use the processor so someone else can have a go. Normally this is fine, especially for long waits, like waiting for IO, loads of other processes can run on the CPU while you're waiting for the disk spindle to rotate.

当您进行多线程锁定时,如果您尝试获取的资源已经被锁定,您通常会进入睡眠状态并等待它变为空闲状态。当您这样做时,您就放弃了调度程序分配给您使用处理器的剩余时间,以便其他人可以使用。通常这很好,特别是对于长时间的等待,比如等待 IO,当你等待磁盘主轴旋转时,其他进程的负载可以在 CPU 上运行。

However, sometimes, you're waiting for a tiny amount of time. In these cases, you would normally give up your remaining time anyway and wait for all the other threads to do their thing before getting another go.. so you can cheat, instead of waiting, you sit there continually polling in a 'are we nearly there yet?' way. If the lock is only held for a fraction of your remaining time, this becomes a very effective means of waiting, its also very efficient as the scheduler doesn't have to get involved in rearranging all the other threads to use the time you give up if you waited normally.

但是,有时,您正在等待一小段时间。在这些情况下,您通常会放弃剩余的时间,并等待所有其他线程完成它们的任务,然后再进行另一次尝试。有吗?道路。如果锁只持有你剩余时间的一小部分,这将成为一种非常有效的等待方式,它也非常有效,因为调度程序不必参与重新安排所有其他线程来使用你放弃的时间如果你正常等待。

Obviously, if you spin every time you want a lock, you're not going to be very popular, your app will become sluggish and use 100% CPU, but in very small doses, at the right time, it makes the app more responsive.

显然,如果你每次想要锁都旋转,你不会很受欢迎,你的应用程序会变得缓慢并使用 100% 的 CPU,但是在非常小的剂量下,在正确的时间,它会使应用程序更具响应性.

If you're now thinking 'when should I use it?', that's a tricky call to make - if you have a resource that is very often locked and unlocked very quickly, then a spinlock around that instead of a wait is a good idea (and then test your app for performance), if you try spinning for a short time, and then fall back to a normal wait, that's a reasonable way too. But generally, you will never need to use it.

如果你现在在想“我应该什么时候使用它?”,这是一个棘手的调用 - 如果你有一个经常被锁定和解锁非常快的资源,那么围绕它的自旋锁而不是等待是一个好主意(然后测试您的应用程序的性能),如果您尝试旋转一小段时间,然后恢复正常等待,这也是一种合理的方式。但一般来说,您永远不需要使用它。

回答by Jon Skeet

The purpose is to do a "cheap" wait if you believe that the condition you're waiting for will come true very, very soon. Normally if you're waiting for something, you let the thread go to sleep and the processor/OS will context switch to another thread. Context switches aren't particularly cheap, so if you have advanced knowledge of the situation and believe it's cheaper to wait than to context switch, you spin wait.

如果您相信您正在等待的条件会很快实现则目的是进行“廉价”等待。通常,如果您正在等待某事,您会让线程进入睡眠状态,处理器/操作系统将上下文切换到另一个线程。上下文切换并不是特别便宜,因此如果您对情况有深入的了解并且认为等待比上下文切换便宜,那么您可以旋转等待。

My advice: if you need to ask, you don't need to use it. (I've never wanted it myself.) Basically it's one of those things which is really useful in a very few situations, but most people should leave well alone.

我的建议:如果你需要问,你不需要使用它。(我自己从不想要它。)基本上,它是在极少数情况下真正有用的东西之一,但大多数人应该远离它。

回答by Jason Evans

As a side note, Microsoft have gotten rid of the thread dispatcher spinlock mechanism from Windows 7, since it didn't scale well for multicore CPU's. Have a look at this:

附带说明一下,Microsoft 已经从 Windows 7 中删除了线程调度程序自旋锁机制,因为它不能很好地扩展到多核 CPU。看看这个

回答by Konrad Rudolph

As far as I'm concerned (and I'm happy for corrections!), the only use of spin waits is when implementing a locking or inter-thread-callback mechanism. And neither should be done manually (usually), since they already exist.

就我而言(我很高兴进行更正!),自旋等待的唯一用途是在实现锁定或线程间回调机制时。并且两者都不应该手动完成(通常),因为它们已经存在。

When you've locked a resource and another thread requests synchronized access to it, it basically has to wait for the first thread to finish using it. This waiting can be done by simply spinning in a loop (or otherwise sleeping + context switching as mentioned by Jon).

当您锁定一个资源并且另一个线程请求同步访问它时,它基本上必须等待第一个线程完成使用它。这种等待可以通过简单地循环旋转来完成(或者 Jon 提到的睡眠 + 上下文切换)。