Click here to Skip to main content
Click here to Skip to main content

Sleeping for Zero Seconds

, 11 Nov 2009
Rate this:
Please Sign up or sign in to vote.
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.Thread.Sleep(0);or if you prefer to see the native code version:Sleep(0);The sleep function is often used to insert delays into code.

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.

Thread.Sleep(0);

or if you prefer to see the native code version:

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)

About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.
 
For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.
 
While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.
 
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid