What is Spinlock?
A lock that waits by repeatedly checking the lock variable in a loop. Fast for short waits, but it keeps using the CPU while waiting.
Japanese name: スピンロック
A lock that waits by repeatedly checking the lock variable in a loop. Fast for short waits, but it keeps using the CPU while waiting.
Japanese name: スピンロック
A spinlock is a shared variable, typically 0 when free and 1 when held. To acquire it, a thread uses an atomic read-modify-write instruction such as test-and-set, swap or compare-and-swap, which sets the variable to 1 and returns the old value in one indivisible step; on RISC-V this is an atomic swap or a load-reserved/store-conditional pair. If the old value was 0, the thread owns the lock; otherwise it tries again in a loop. Releasing simply stores 0. Acquire and release must also act as memory barriers, so that accesses inside the critical section stay between them.
Retrying the atomic instruction in a tight loop performs poorly on multicore chips. Every atomic operation needs exclusive ownership of the cache line, so waiting cores keep pulling the line into the modified (M) state and invalidating each other's copies, flooding the coherence fabric. The test-and-test-and-set pattern waits with ordinary loads instead: each waiter reads its own shared copy in its cache, generating no traffic, and attempts the atomic operation only when it sees the lock become free. Exponential backoff further reduces contention, and ticket or queue-based locks also add fairness in order of arrival.
A mutex that sleeps puts a waiting thread into a blocked state and hands the CPU to other threads; the operating system wakes it when the lock is released. That wastes no CPU time but costs a system call, scheduling and a context switch, typically several microseconds. A spinlock avoids that overhead entirely and wins when the lock is held for only a short time, shorter than a sleep and wake cycle would take. For long hold times the balance reverses, so many library mutexes spin briefly first and then sleep, combining the strengths of both.
Spinlocks are common inside operating system kernels, for short critical sections on multiprocessors and for data shared with interrupt handlers, which cannot sleep. Code holding a spinlock must not sleep, and kernels usually disable preemption, and interrupts where needed, while it is held, so that an interrupt handler on the same core cannot spin forever on a lock its own core holds. On a single-core system a user-level spinlock is especially wasteful: the holder cannot run to release the lock while the waiter spins, so the waiter burns its entire time slice.
Answer: B. A lock that waits by repeatedly checking the lock variable in a loop until it is free
A spinlock tries to acquire the lock with an atomic instruction and, if that fails, immediately tries again, over and over. There is no cost of calling the OS to sleep and wake, so it is fast when the lock is held only very briefly. It is common inside OS kernels and for mutual exclusion with interrupt handlers. Its drawback is that it keeps using the CPU while waiting.
Answer: A. When the lock is held for a long time, so waiting threads waste CPU time spinning
A thread waiting on a spinlock keeps using the CPU until the lock is free. If the lock is held for long, all that CPU time is wasted and other threads are held back. On a single core it is worse: the lock cannot be released unless the holder runs, so the waiter spins until its time slice ends. Long waits call for a mutex that sleeps, and some locks spin briefly before sleeping.
A spinlock waits by looping on the lock variable and keeps the CPU busy, which is efficient only for very short waits. A mutex puts the waiting thread to sleep and lets other threads run, which suits longer waits but adds the cost of sleeping, waking and context switching.
Use one when the critical section is very short and threads run on different cores, or in kernel contexts such as interrupt handlers that are not allowed to sleep. Avoid it when the lock may be held for long or on single-core systems.
It is a spinlock technique in which waiters repeatedly read the lock with ordinary loads from their own caches and attempt the atomic test-and-set only when the lock appears free. This avoids constant cache-line invalidations and greatly reduces coherence traffic.
Spinlock appears in 4 practice questions across 1 topic, including Multicore & Synchronization.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.