What is ABA problem?

The problem that when a value changes from A to B and back to A, a CAS comparing only values fails to notice and succeeds. Lock-free data structures must guard against it.

Field: Multicore & Synchronization

Japanese name: ABA問題

How the ABA problem arises

Compare-and-swap (CAS) writes a new value only if a location still holds an expected value, and it compares values, not history. Consider a lock-free stack whose head points to node A, with A's next pointer pointing to B. Thread 1 reads head = A and next = B, then is preempted. Thread 2 pops A, pops B, and pushes A back, or pushes a new node that happens to reuse A's address. Thread 1 resumes, its CAS(head, A, B) succeeds because head is still A, and head now points to B, a node that is no longer in the stack.

Why memory reuse makes it likely

The problem is most common in languages with manual memory management. When a node is freed, the allocator readily hands the same address to the next allocation, so a pointer value can return to A even though it refers to a different node. In garbage-collected languages a node cannot be reclaimed while a thread still holds a reference to it, which largely removes the address-reuse form of ABA, although the logical form can still occur if the same node is deliberately reinserted.

Standard countermeasures

The classic fix pairs the pointer with a modification counter and compares both in one atomic operation, using a double-width CAS or spare pointer bits; every update increments the counter, so A with tag 5 differs from A with tag 7. Load-linked/store-conditional (LL/SC, called LR/SC in RISC-V) fails if the location was written at all since the load, even when the value is the same. Safe memory reclamation schemes, such as hazard pointers and epoch-based reclamation, prevent a node from being freed and reused while any thread may still access it.

ABA and lock-free progress

A lock-free algorithm guarantees that some thread always makes progress even if others stall, because a CAS fails only when another thread's update succeeded. That progress guarantee says nothing about correctness, and ABA is the classic way a seemingly correct CAS loop corrupts a data structure. It is hard to catch in testing because it needs a specific interleaving plus memory reuse, so lock-free code should be designed with an explicit ABA strategy from the start.

Try it: questions about ABA problem

Which describes the ABA problem that affects lock-free data structures built with CAS?

  1. ATwo threads A and B can only run alternately
  2. BWhen a value changes from A to B and back to A, CAS fails to notice the change and succeeds
  3. CTwo locks A and B are taken in opposite orders
Show answer and explanation

Answer: B. When a value changes from A to B and back to A, CAS fails to notice the change and succeeds

Suppose a linked list's head pointer is A. While one thread reads A and prepares a CAS, other threads remove A, do work involving B, and reinsert a node at the same address A. The CAS succeeds because the value is still A, but the list has already changed and its structure is corrupted. It is prevented by pairing the pointer with a modification counter compared together, or by using LR/SC.

FAQ

What is the ABA problem in simple terms?

A thread sees value A, another thread changes it to B and back to A, and the first thread's compare-and-swap succeeds as if nothing had happened. The value matches, but the state behind it has changed.

How does LL/SC avoid the ABA problem?

Store-conditional succeeds only if no write has occurred to the location since the matching load-linked, regardless of the value written. A change from A to B and back to A therefore still makes the store fail.

Does a version counter fully solve ABA?

In practice yes, as long as the counter is wide enough that it cannot wrap around to the same value while a thread is stalled. A very narrow counter can in principle wrap and reintroduce the problem.

Questions that mention ABA problem (2)

ABA problem appears in 3 practice questions across 1 topic, including Multicore & Synchronization.

  1. What does the atomic CAS (compare-and-swap) instruction do?
  2. What property does a lock-free algorithm have?

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