Is there an alternative for sleep() in C?

Manoj Doubts picture Manoj Doubts · Nov 5, 2008 · Viewed 51.4k times · Source

In traditional embedded programming, we will give a delay function like so:

for(i=0;i<255;i++)
   for(j=0;j<255;j++);

In the microprocessor's view, is this how the sleep() function works?

Is there an alternative for the sleep() function in C?

Answer

Chris Jester-Young picture Chris Jester-Young · Nov 5, 2008

The kind of loop you describe is called a "busy wait". In real operating systems, sleeping does not cause a busy wait; it tells the operating system to not schedule the process in until the sleep period is over.