Skip to main content

Differences between sleep(0) and sleep(1)

Well, sleep(ms) gives away the processor for at least ms milliseconds.

However, there are two special values for ms, 0 and 1.

sleep(0) gives away the processor for the current timeslice, but only to threads of equal or higher priority. If there are none of these, it returns immediately without giving away the timeslice.

sleep(1) always gives away the current timeslice, even to lower priority threads, or even to the idle state if there are no threads to execute. It always sleeps for at least 1ms, rounded up to the next OS scheduler invocation, it never returns immediately.