What is Load-reserved/store-conditional (LR/SC)?

A pair of instructions, also called LL/SC: if another core writes the address after the load-reserved, the store-conditional fails, enabling atomic updates.

Field: Multicore & Synchronization

Japanese name: LR/SC

How the pair works

Load-reserved (LR) reads a memory word and at the same time registers a reservation on that address, usually covering its cache line. The program computes a new value in registers, then store-conditional (SC) attempts to write it. SC writes only if the reservation is still intact and returns a status code indicating success or failure; on RISC-V, zero means success. Software wraps the sequence in a retry loop. RISC-V calls the pair lr and sc, Arm provides equivalent load-exclusive and store-exclusive instructions, and the general name is LL/SC.

What breaks a reservation

The main cause of failure is another core writing to the reserved location between the LR and the SC. The core detects this through cache coherence: the write invalidates its copy of the line and clears the reservation. The reservation may also be cleared by an interrupt or context switch, another SC, or eviction of the line, so SC can fail even without a real conflict. Correct code must always be prepared to retry, and RISC-V guarantees eventual success only for short loops that follow specified restrictions.

LR/SC vs. CAS and the ABA problem

Compare-and-swap (CAS) checks whether memory still holds an expected value. If another thread changes a value from A to B and back to A, CAS cannot tell and succeeds, which can corrupt lock-free structures such as linked lists: the ABA problem. LR/SC checks whether any write happened, not what the value is, so an A-B-A sequence still breaks the reservation. LR/SC can also build any single-word read-modify-write in software, including CAS itself and fetch-and-add. Its weaknesses are spurious failures and possible livelock under heavy contention.

Why load-store architectures favor it

A single CAS instruction must read, compare and write memory in one step, which fits awkwardly into a pipeline where each instruction performs at most one memory access. LR/SC keeps that load/store discipline: LR behaves like a load plus a reservation register, and SC like a store gated by that register. No bus locking is needed, because the coherence protocol already tells the core when another processor writes the line. For this reason the pair has long been used in RISC instruction sets, often alongside single-instruction atomics.

Try it: questions about Load-reserved/store-conditional (LR/SC)

With an instruction pair such as RISC-V LR/SC (load-reserved / store-conditional), when does the SC fail?

  1. AWhen the value SC writes equals the value LR read
  2. BWhen the reservation is lost, for example because another core wrote that address between the LR and the SC
  3. CWhenever any other instruction is executed between LR and SC
Show answer and explanation

Answer: B. When the reservation is lost, for example because another core wrote that address between the LR and the SC

LR reads a value and at the same time "reserves" the address. When SC later writes, it succeeds if the reservation is intact and fails without writing if the reservation was lost, for example because another core wrote there. Whereas CAS checks for a matching value, LR/SC checks whether any write happened, so it also detects a value that changed and changed back. Arm has a similar instruction pair.

FAQ

When does a store-conditional fail?

It fails when the reservation set by the load-reserved has been lost, most commonly because another core wrote the address, but also possibly due to an interrupt, context switch or cache eviction.

Does LR/SC solve the ABA problem?

Yes. LR/SC detects any intervening write to the reserved location, so a value that changes and changes back still causes the store-conditional to fail, unlike compare-and-swap.

What is the difference between LL/SC and LR/SC?

They are the same idea under different names: load-linked/store-conditional is the traditional term, and load-reserved/store-conditional is the RISC-V name.

Questions that mention Load-reserved/store-conditional (LR/SC) (2)

Load-reserved/store-conditional (LR/SC) appears in 3 practice questions across 1 topic, including Multicore & Synchronization.

  1. Which describes an atomic instruction?
  2. Which describes the ABA problem that affects lock-free data structures built with CAS?

Books on Multicore & Synchronization Ad

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.

Back to the glossary