Code on a screen, representing a spinlock.

A lock that keeps checking until it is free.

What a spinlock is

A spinlock is a type of lock used to protect shared data, where a thread trying to acquire the lock simply waits in a loop - spinning - while repeatedly checking whether the lock has become available. Rather than giving up the processor while it waits, the thread stays active and keeps testing the lock until it can take it. This busy waiting is what defines a spinlock.

Locks and shared data

When multiple threads share data, a lock ensures that only one accesses the protected data at a time, preventing conflicts. A thread must acquire the lock before using the data and release it afterward. If another thread already holds the lock, the waiting thread must somehow wait. A spinlock is one approach to this waiting, in which the thread keeps checking in a tight loop.

Busy waiting explained

The characteristic behavior of a spinlock is busy waiting: the waiting thread remains active, continuously checking whether the lock is free instead of pausing. This keeps the thread ready to proceed the instant the lock is released, with little delay. The trade-off is that while spinning, the thread is using the processor without doing useful work, simply waiting for its turn.

When spinlocks are useful

Spinlocks can be efficient when the wait is expected to be very short. If the lock will be released almost immediately, spinning briefly avoids the overhead of pausing and later resuming the thread, letting it continue with minimal delay. In such cases, the small amount of wasted effort from spinning is outweighed by the speed of grabbing the lock as soon as it is free.

The trade-offs

The downside of a spinlock appears when the wait is long. A thread that spins for a long time wastes processor time that other work could have used, since it is busy checking rather than doing anything productive. For this reason, spinlocks are best suited to short waits, and other waiting methods are preferred when a thread might be blocked for a longer period.

Why it matters

Spinlocks are an important tool in coordinating access to shared data among threads. Understanding what a spinlock is - a lock that waits by repeatedly checking in a loop - clarifies one approach to synchronization and its trade-offs. For anyone learning about concurrency and how threads cooperate, it is a valuable concept to understand.

See also: What is a mutex? Explained, What is a semaphore? Explained.

Sources

Published date reflects the original event date (2025-04-22). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.

Skillo Staff

Skillo Staff

All stories by: Skillo Staff

Leave a Reply