Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Sleeping for Zero Seconds

4.83/5 (4 votes)
11 Nov 2009CPOL2 min read 9.1K  
Sleeping for zero seconds

I was asked about something that appeared to be an oddity in some example code that I posted online. The code in question was a single call to the sleep function.

C#
Thread.Sleep(0);

or if you prefer to see the native code version:

C#
Sleep(0);

The sleep function is often used to insert delays into code. But what does it mean to insert a delay of zero? This will make more sense if we first take a look at the API documentation. I pulled this description from the native API documentation for the sleep function:

This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds.

So there are two actions performed by calling this function:

  1. Thread relinquishes its time slice
  2. Thread becomes unrunnable for the interval specified

Sleeping for zero seconds will cause the thread to relinquish control, but it will remain runnable. What does it mean for a thread to be runnable? To understand this, one must understand how multitasking works. At its simplest level, a single core processor can only run one thread or program at a time. The illusion of multiple programs doing something at the same time is maintained by rapidly switching control from one program to another. When the operating system is deciding which thread to switch to next one of the factors is which threads are runnable. A thread can become unrunnable voluntarily (by calling the sleep function or using other thread related APIs) or the thread may make a call that requires some time for an event to complete, such as an IO request to a drive or network device. When the thread is made runnable again, then it is reintroduced as a candidate thread for receiving time to execute.

When a program calls Sleep(0), it is never made unrunnable. It just allows for the operating system to go ahead and schedule the next thread for execution.

When is it Appropriate to Use Sleep(0)?

Most often, I use Sleep(0) in code in which the main program loop is not being driven by messages and events. For programs that are driven by messages and events, if the program is not receiving any messages (such as notification of user actions, GPS coordinates changing, timers expiring, so on), then the program is not doing anything. Programs of this nature spend a majority of their life not doing anything. Though the user typically won't perceive these programs that way. Programs that are not dependent on messages and events to run (typically games or media intensive applications) execute continuously and can quickly consume CPU bandwith. Throwing the Sleep(0) into the main loop of such a program will give other programs a chance to execute (as opposed to appearing locked up due to another process monopolizing the CPU) and in some cases, the Sleep(0) can help better preserve battery life.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)