A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Test-and-Set and Atomic Primitives

College Depth 108 in the knowledge graph ☐ I know this ☆ Set as goal
419prerequisites beneath it
See this on the map →
Software-Only Mutual Exclusion SolutionsAtomic Operations and Compare-and-Swap+1 more→
synchronization atomic hardware

Core Idea

Test-and-set, compare-and-swap, and similar atomic operations read and modify memory in a single indivisible instruction. These enable efficient lock implementation without polling or context switches, providing the hardware foundation for high-level synchronization primitives.

Explainer

From studying software mutual exclusion solutions like Peterson's algorithm, you know the fundamental problem: two threads both want to enter a critical section, and we need a protocol that guarantees only one gets in at a time. Software solutions work, but they rely on careful sequences of reads and writes to shared variables — and on modern processors with out-of-order execution and caching, these sequences can break in subtle ways. The hardware solution is to give the processor a single instruction that does two things at once, indivisibly.

Test-and-set is the simplest such instruction. It reads the current value of a memory location, sets it to 1 (or true), and returns the old value — all in one atomic step. No other processor or thread can intervene between the read and the write. To build a lock, you initialize a shared variable `lock` to 0 (unlocked). A thread that wants to enter the critical section executes test-and-set on `lock`. If the old value was 0, the thread knows it just changed the lock from unlocked to locked — it now holds the lock and can proceed. If the old value was 1, someone else already holds the lock, and the thread must try again (spinning in a loop until test-and-set returns 0).

Compare-and-swap (CAS) is a more flexible variant. It takes three arguments: a memory location, an expected old value, and a new value. It atomically checks whether the location holds the expected value; if so, it replaces it with the new value and reports success; if not, it does nothing and reports failure. CAS can implement not just locks but also lock-free data structures — for example, you can atomically update a linked list's head pointer only if no other thread changed it since you last read it. This "optimistic" approach avoids locking entirely in the uncontended case, which can dramatically improve performance on multi-core systems.

The key insight is that these operations derive their power from hardware guarantees. The processor's memory bus or cache coherence protocol ensures that when one core executes test-and-set, no other core can access that memory location until the operation completes. This atomicity cannot be achieved by software alone on modern hardware — it requires dedicated instructions like x86's `XCHG`, `LOCK CMPXCHG`, or ARM's `LDREX`/`STREX` pairs. These primitives are the foundation on which all higher-level synchronization — semaphores, mutexes, condition variables — is ultimately built.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Process Model Formalization → Context Switching and CPU Dispatch → CPU Scheduling Fundamentals → Round-Robin (RR) Scheduling → First-Come-First-Served (FCFS) Scheduling → Scheduling Fairness and Starvation Prevention → Thread Scheduling and Coordination → Semaphores → Counting Semaphores and Resource Pools → Producer-Consumer Problem: Classic Synchronization → Barrier Synchronization Primitives → Test-and-Set and Atomic Primitives

Longest path: 109 steps · 419 total prerequisite topics

Prerequisites (3)

Leads To (0)

No topics depend on this one yet.