57

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)?

tshepang
  • 12,111
  • 21
  • 91
  • 136
bdhar
  • 21,619
  • 17
  • 70
  • 86
  • 8
    With C++11, you have a better platform independent way to do that: [`std::this_thread::yield()`](http://en.cppreference.com/w/cpp/thread/yield)! – iammilind Dec 14 '14 at 19:01

8 Answers8

61

According to MSDN's documentation for 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 something will 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.

Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
  • Is it guaranteed windows to put any higher or same level priority thread on the cpu after calling sleep(0)? And how long does it take in microseconds or nanoseconds? – tolgayilmaz Sep 20 '18 at 09:35
18

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.

Source

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
14

I'm afraid I can't improve on the MSDN docs here

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.

Please also note (via upvote) the two useful answers regarding efficiency problems here.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
14

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.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • 4
    I don't see how it could ever make sense to put a Sleep(0) in a loop that is also supposed to be fast. – sellibitze Sep 16 '10 at 14:38
  • sellibitze: I mean, one iteration is fast, and not the whole loop. – Alex F Sep 16 '10 at 14:48
  • 1
    @sellibitze: Consider hybrid sleep/busy-wait. This is pretty common for non-mission critical timing sensitive code that does not want to hog the CPU, though the heuristic is usually something more sophisticated such as only sleep if you expect the next iteration's deadline to be > n-many ms in the future. – Andon M. Coleman Nov 06 '15 at 17:52
3

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.

On Linux there's a specific command for this: sched_yield() as from the man pages:

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().

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 calling sched_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.

fduff
  • 3,671
  • 2
  • 30
  • 39
2

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).

norm
  • 21
  • 1
1

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.

I've given some details in https://stackoverflow.com/a/11456112/1504523

Community
  • 1
  • 1
Arno
  • 4,994
  • 3
  • 39
  • 63
0

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.

Nick Sotiros
  • 2,194
  • 1
  • 22
  • 18