What is Atomic instruction?
An instruction that performs a memory read and write as one operation no other core can interrupt, such as CAS, fetch-and-add and LR/SC.
Japanese name: アトミック命令
An instruction that performs a memory read and write as one operation no other core can interrupt, such as CAS, fetch-and-add and LR/SC.
Japanese name: アトミック命令
A statement like counter++ is executed as three steps, a load, an add and a store (even on processors where it is a single instruction, it is split into three steps internally). If two cores run it at the same time, both can load the same old value and both store old + 1, losing one increment. An atomic instruction performs the whole read-modify-write as one indivisible operation: no other core's access to that location can slip in between, and other cores observe only the value before or after it. This guarantee is the foundation for locks, counters and lock-free data structures on multicore processors.
Test-and-set and atomic swap write a new value and return the old one. Fetch-and-add adds to memory and returns the previous value. Compare-and-swap (CAS) writes a new value only if memory still holds an expected value, reporting whether it succeeded. Load-reserved/store-conditional (LR/SC) splits the operation into a reserving load and a store that fails if the reservation was lost. RISC-V also provides atomic memory operation (AMO) instructions such as amoadd and amoswap, while x86 applies a LOCK prefix to instructions such as cmpxchg and xadd.
A simple approach locks the shared bus for the duration of the operation. Most cores instead use the cache coherence protocol: the core obtains exclusive ownership of the cache line, in the Modified or Exclusive state of a MESI-style protocol, and delays other cores' requests for that line until the read-modify-write finishes. An atomic is therefore much slower than a plain cached load, and under contention the line bounces between cores. Atomics also interact with memory ordering; on x86, locked instructions act as full memory barriers.
A spinlock tries to set a lock variable with an atomic instruction and retries in a loop if it is already taken. Retrying the atomic write directly causes heavy coherence traffic, because every attempt demands exclusive ownership. The test-and-test-and-set pattern spins with ordinary reads on a shared copy in the local cache and attempts the atomic only when the lock appears free. Lock-free structures use CAS or LR/SC directly, but CAS-based designs must guard against the ABA problem, where a value changes and changes back unnoticed.
Answer: A. An instruction that performs a memory read and write as a single operation that no other core can interrupt partway
An atomic instruction performs a read, compute and write sequence such that no other core's access can come in between. Other cores see only the state before or after the operation. Examples include CAS (compare-and-swap), fetch-and-add and RISC-V's LR/SC and AMO instructions, which are the building blocks of locks and lock-free data structures.
Answer: C. Reads can wait inside the core's own cache, greatly reducing coherence traffic as the line bounces between cores
An atomic instruction involves a write, so waiting threads that keep executing it fight to own the line in the M state and keep invalidating others' copies. With ordinary reads, each core just keeps reading its Shared copy in its own cache, generating no traffic. Only when the lock is released and the value changes does a thread try to grab it with an atomic instruction.
An atomic instruction makes a single read-modify-write on one memory location indivisible in hardware; a lock is a software construct, built from atomics, that protects a longer section of code touching many locations.
Compare-and-swap writes a new value to a memory location only if it still contains an expected value, and reports success or failure, all as one atomic operation.
The core must gain exclusive ownership of the cache line and often enforce memory ordering, and when several cores contend for the same line it is repeatedly transferred and invalidated between them.
Atomic instruction appears in 3 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.