SwitchToThread VS睡眠(1)睡眠、SwitchToThread、VS

2023-09-03 00:07:06 作者:擅长可爱

我不知道什么叫之间的实际差异的Thread.Sleep(1),并调用SwitchToThread(如果我们忽略了,目前不能由BCL曝光)。

I'm wondering what's the actual difference between calling Thread.Sleep(1) and calling SwitchToThread (if we ignore that it's currently not exposed by the BCL).

乔·达菲提到的他的岗位是:

的KERNEL32!SwitchToThread API不会出现问题,睡眠(0)和休眠(1)做的。 (关于调度程序的行为)

"The kernel32!SwitchToThread API doesn't exhibit the problems that Sleep(0) and Sleep(1) do." (regarding the scheduler's behavior)

为什么会睡不着觉的行为完全一样SwitchToThread?为什么这种差异的存在,以及到底有什么好处呢? (如果有的话..)

Why won't Sleep behave exactly like SwitchToThread? Why this differentiation exist, and for what is it good for? (if at all..)

推荐答案

有两点不同。第一个被提及的MSDN文档的 SwitchToThread :

There are two differences. The first is mentioned in the MSDN docs for SwitchToThread:

执行的产率被限制在调用线程的处理器。操作系统将不会切换执行到另一处理器,即使该处理器处于空闲状态或正在运行低优先级的线程。

The yield of execution is limited to the processor of the calling thread. The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority.

睡眠(0),将允许在其它处理器的线程来运行,也是如此。

Sleep(0) will allow threads on other processors to run, as well.

SwitchToThread只产生一个单独的线程调度方面,也是如此。睡眠,另一方面,具有用于它等待多个条件。该文档的 SleepEx 阐明这一点的详细信息:

SwitchToThread only yields to a single thread scheduling context, as well. Sleep, on the other hand, has multiple conditions for which it waits. The docs for SleepEx spell this out in detail:

* An I/O completion callback function is called
* An asynchronous procedure call (APC) is queued to the thread.
* The time-out interval elapses

这将产生多个线程。

在一般情况下,睡眠(0),将更有可能产生一个时间片,并且总是生成到操作系统,即使没有其他等待的线程。这就是为什么在一个循环中增加一个休眠(0)将采取处理器的使用量从100%(每核心)到接近0%,在许多情况下。 SwitchToThread不会,除非另一个线程正在等待一个时间段。

In general, Sleep(0) will be much more likely to yield a timeslice, and will ALWAYS yield to the OS, even if there are no other threads waiting. This is why adding a Sleep(0) in a loop will take the processor usage from 100% (per core) to near 0% in many cases. SwitchToThread will not, unless another thread is waiting for a time slice.