concept

Spinlock

A spinlock is a low-level synchronization primitive used in concurrent programming to protect shared resources by causing a thread to repeatedly check (or 'spin') in a loop until the lock becomes available, rather than blocking and yielding the CPU. It is commonly implemented in operating systems and multithreaded applications where lock contention is expected to be short-lived, as it avoids the overhead of context switching. However, it can waste CPU cycles if held for long periods, making it suitable only for specific high-performance scenarios.

Also known as: Spin lock, Busy-wait lock, Spin-wait lock, Spin-loop, Spin mutex
🧊Why learn Spinlock?

Developers should learn and use spinlocks when implementing low-latency systems, such as in kernel development, real-time applications, or high-frequency trading, where the cost of thread blocking and context switching outweighs the CPU overhead of spinning. They are particularly useful in scenarios with very short critical sections and high core counts, as they minimize latency by keeping threads active on the CPU. However, they should be avoided in user-space applications with unpredictable lock hold times to prevent CPU starvation and inefficiency.

Compare Spinlock

Learning Resources

Related Tools

Alternatives to Spinlock