What Is Busy Spinning? When Will You Use Busy Spinning as Waiting Strategy?

By | March 6, 2023

Busy spinning is a waiting strategy where a thread repeatedly checks for a condition to become true instead of blocking and waiting for the condition to become true. Busy spinning can be implemented using a loop that checks the condition repeatedly.

Here’s an example of busy spinning:

while (!condition) {

  // Do nothing

}

Busy spinning can be useful in situations where we expect the condition to become true quickly and want to avoid the overhead of blocking and unblocking a thread. For example, busy spinning can be used in cases where we are waiting for a lock to become available or waiting for a flag to be set by another thread.

However, busy spinning can also have drawbacks. If the condition takes a long time to become true, busy spinning can consume a lot of CPU resources and cause the application to become unresponsive. Busy spinning can also cause cache thrashing, where multiple threads are competing for the same cache line, leading to performance degradation.

In general, we should use busy spinning as a waiting strategy only in cases where we expect the condition to become true quickly and the overhead of blocking and unblocking a thread is significant. If we are unsure about the expected wait time or if the wait time is likely to be long, we should use other waiting strategies, such as blocking or timed blocking.

Leave a Reply

Your email address will not be published. Required fields are marked *