C++ 睡眠的意义(0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727420/
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
Significance of Sleep(0)
提问by bdhar
I used to see Sleep(0)
in some part of my code where some infinite/long while
loops are available. I was informed that it would make the time-slice available for other waiting processes. Is this true? Is there any significance for Sleep(0)
?
我曾经Sleep(0)
在我的代码的某些部分看到一些无限/长while
循环可用。我被告知它将使时间片可用于其他等待进程。这是真的?有什么意义Sleep(0)
吗?
回答by Nick Meyer
According to MSDN's documentation for Sleep:
根据 MSDN 的Sleep文档:
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
零值会导致线程将其时间片的剩余部分放弃给准备运行的任何其他线程。如果没有其他线程准备运行,则函数立即返回,线程继续执行。
The important thing to realize is that yes, this gives other threads a chance to run, but if there are none ready to run, then your thread continues -- leaving the CPU usage at 100% since somethingwill always be running. If your while loop is just spinning while waiting for some condition, you might want to consider using a synchronization primitive like an event to sleep until the condition is satisfied or sleep for a small amount of time to prevent maxing out the CPU.
要意识到的重要一点是,是的,这给了其他线程一个运行的机会,但是如果没有准备好运行的线程,那么您的线程将继续——将 CPU 使用率保持在 100%,因为某些东西将始终在运行。如果您的 while 循环只是在等待某个条件时自转,您可能需要考虑使用同步原语(如事件)来休眠直到条件满足或休眠一小段时间以防止 CPU 最大化。
回答by Sjoerd
Yes, it gives other threads the chance to run.
是的,它给了其他线程运行的机会。
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
零值会导致线程将其时间片的剩余部分放弃给准备运行的任何其他线程。如果没有其他线程准备运行,则函数立即返回,线程继续执行。
回答by Steve Townsend
I'm afraid I can't improve on the MSDN docshere
恐怕我无法改进这里的MSDN 文档
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
Windows XP/2000: A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. This behavior changed starting with Windows Server 2003.
零值会导致线程将其时间片的剩余部分放弃给准备运行的任何其他线程。如果没有其他线程准备运行,则函数立即返回,线程继续执行。
Windows XP/2000:零值会导致线程将其时间片的剩余部分放弃给准备运行的任何其他具有相同优先级的线程。如果没有其他同等优先级的线程准备运行,则函数立即返回,线程继续执行。此行为从 Windows Server 2003 开始发生了变化。
Please also note (via upvote) the two useful answers regarding efficiency problems here.
还请注意(通过投票)这里关于效率问题的两个有用答案。
回答by Alex F
Be careful with Sleep(0), if one loop iteration execution time is short, this can slow down such loop significantly. If this is important to use it, you can call Sleep(0), for example, once per 100 iterations.
小心使用 Sleep(0),如果一个循环迭代执行时间很短,这会显着减慢此类循环的速度。如果这对使用它很重要,您可以调用 Sleep(0),例如,每 100 次迭代一次。
回答by fduff
Sleep(0);
At that instruction, the system scheduler will check for any other runnable threads and possibly give them a chance to use the system resources depending on thread priorities.
Sleep(0);
在该指令中,系统调度程序将检查任何其他可运行的线程,并可能根据线程优先级为它们提供使用系统资源的机会。
On Linux there's a specific command for this: sched_yield()
as from the man pages:
在 Linux 上有一个特定的命令:sched_yield()
从手册页:
sched_yield()
causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.If the calling thread is the only thread in the highest priority list at that time, it will continue to run after a call to
sched_yield()
.
sched_yield()
导致调用线程放弃 CPU。线程被移动到队列的末尾以获取其静态优先级,并且一个新线程开始运行。如果当时调用线程是最高优先级列表中的唯一线程,则在调用 之后它将继续运行
sched_yield()
。
with also
还有
Strategic calls to
sched_yield()
can improve performance by giving other threads or processes a chance to run when (heavily) contended resources (e.g., mutexes) have been released by the caller. Avoid callingsched_yield()
unnecessarily or inappropriately (e.g., when resources needed by other schedulable threads are still held by the caller), since doing so will result in unnecessary context switches, which will degrade system performance.
sched_yield()
当(严重)竞争资源(例如互斥体)被调用者释放时,策略性调用可以通过给其他线程或进程一个运行的机会来提高性能。避免sched_yield()
不必要或不恰当的调用(例如,当其他可调度线程所需的资源仍由调用者持有时),因为这样做会导致不必要的上下文切换,从而降低系统性能。
回答by norm
In one app....the main thread looked for things to do, then launched the "work" via a new thread. In this case, you should call sched_yield() (or sleep(0)) in the main thread, so, that you do not make the "looking" for work, more important then the "work". I prefer sleep(0), but sometimes this is excessive (because you are sleeping a fraction of a second).
在一个应用程序中......主线程寻找要做的事情,然后通过一个新线程启动“工作”。在这种情况下,您应该在主线程中调用 sched_yield()(或 sleep(0)),这样您就不会“寻找”工作,而不是“工作”。我更喜欢 sleep(0),但有时这太过分了(因为您只睡了几分之一秒)。
回答by Arno
Sleep(0) is a powerful tool and it can improve the performance in certain cases. Using it in a fast loop might be considered in special cases. When a set of threads shall be utmost responsive, they shall all use Sleep(0) frequently. But it is crutial to find a ruler for what responsive means in the context of the code.
Sleep(0) 是一个强大的工具,它可以在某些情况下提高性能。在特殊情况下可能会考虑在快速循环中使用它。当一组线程响应速度最快时,它们都应频繁使用 Sleep(0)。但在代码的上下文中找到响应意味着什么的标尺是至关重要的。
I've given some details in https://stackoverflow.com/a/11456112/1504523
回答by Nick Sotiros
I am using using pthreads and for some reason on my mac the compiler is not finding pthread_yield() to be declared. But it seems that sleep(0) is the same thing.
我正在使用 pthreads,由于某种原因,在我的 Mac 上编译器没有找到要声明的 pthread_yield()。但似乎 sleep(0) 是一回事。