Busy loop
Jump to navigation
Jump to search
Definition
A busy loop is a loop that continuously polls for an event. An alternative to this inefficient use of resources, for example, is to ask for an interrupt or sleep until the event occurs.
Use Cases
Busy loops, while expensive, can be used efficiently in some cases. You can implement a mutex like so, using a busy loop:
int lock(mutex *mutex) {
// Wait for the mutex to become free.
while(mutex->in_use);
mutex->in_use = 1;
return 0;
}
Busy loops can also be used to poll hardware, although it is inefficient. This is sometimes the only option for some hardware that does not support interrupts (bless its heart).