Cognitive Scaffold

Preparing your thinking workspace

arrow_back_ios_new
MENTAL MODEL · M6297

Spinlock

Spinlock
SystemsHigh supportSystems Theory
Included
account_tree

Updated 2026-07-28

Loading revision record…

Summary

A spinlock is a lock used in computer science for multi-threaded synchronization, where a thread repeatedly checks whether the lock variable is available. Since the thread remains executing during this process, it is a form of busy-waiting. Once a spinlock is acquired, the thread holds it until explicitly released. Spinlocks avoid the scheduling overhead of process context switching, making them effective in situations where threads block only for very short periods. Therefore, operating system implementations often use spinlocks in many places. The lightweight reader-writer lock (SRW Lock) provided by Windows internally uses spinlocks.

Definition

A spinlock is a lock used in computer science for multi-threaded synchronization, where a thread repeatedly checks whether the lock variable is available. Since the thread remains executing during this process, it is a form of busy-waiting. Once a spinlock is acquired, the thread holds it until explicitly released. Spinlocks avoid the scheduling overhead of process context switching, making them effective in situations where threads block only for very short periods. Therefore, operating system implementations often use spinlocks in many places. The lightweight reader-writer lock (SRW Lock) provided by Windows internally uses spinlocks. Obviously, a single-core CPU is not suitable for spinlocks; here, single-core CPU refers to a single-core, single-threaded CPU, because at any given time only one thread is in the running state. Suppose running thread A finds that it cannot acquire the lock and can only wait for it to be unlocked, but because A does not suspend itself, the thread B holding the lock cannot enter the running state; it can only be scheduled after the time slice allocated to A by the operating system expires. In this case, using a spinlock is very costly. Acquiring and releasing a spinlock actually involves reading and writing the memory or register that stores the spinlock. Therefore, such read/write operations must be atomic.

How to use

A spinlock is a lock used in computer science for multi-threaded synchronization, where a thread repeatedly checks whether the lock variable is available. Since the thread remains executing during this process, it is a form of busy-waiting. Once a spinlock is acquired, the thread holds it until explicitly released. Spinlocks avoid the scheduling overhead of process context switching, making them effective in situations where threads block only for very short periods. Therefore, operating system implementations often use spinlocks in many places. The lightweight reader-writer lock (SRW Lock) provided by Windows internally uses spinlocks.

Mechanism

When a thread cannot acquire the lock, it does not sleep but spins in a busy loop, avoiding context-switch overhead; suitable for very short critical sections and multi-core scenarios.

Practice

In progress 0/4

Practice this model in one real situation:

Check to track your progress (stored locally)
Learning progress0%

Cases

  • Kernel spinlocks protect very short critical sections; short-term locking for high-concurrency counters.

Common pitfalls

  • Using spinlocks on single-core or with long critical sections causes wasted spinning; ignoring priority inversion risks.
Sources

Source support: Explicit

References currently contain only Wikipedia links; primary sources still need review.

  • link
    zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E8%87%AA%E6%97%8B%E9%94%81ZH · Explicit
    verified
account_treeGenealogyexpand_more

RELATED MODELS