What are trade offs for “busy wait” vs “sleep”?

Going to sleep until the scheduler wakes you is the normal/prefered thing to do.

Spinning (the alternative way to wait, without sleeping) is less usual and has the following effects:

  • Keeps the CPU busy, and prevents other threads from using the CPU (until/unless the spinning thread finishes its timeslice and is prempted)

  • Can stop spinning the very moment the thing which you’re waiting for happens (because you’re continuously checking for that event, and you don’t need to take the time it takes to be woken up, because you’re already awake)

  • Doesn’t invoke the CPU istructions required to go to sleep and to wake up again

Spinning can be more efficient (less total CPU) than going to sleep, if the length of the delay is very short (e.g. if the delay is for only as long as it takes to execute 100 CPU instructions).

Leave a Comment