Solving the ABA Problem in Rust with Tagged Pointers

Oleksandr Prokhorenko
2 min readFeb 11, 2025

--

Introduction

Concurrent programming is challenging, especially when designing lock-free data structures. One subtle issue that can arise is the ABA problem, leading to unexpected behavior in compare-and-swap (CAS) operations. In this post, we’ll explore the ABA problem and how to solve it in Rust using idiomatic and efficient techniques.

📺 Series Overview

This blog post is the first in a three-part series on solving the ABA problem in Rust. In this series we will cover:

  1. Part 1: Tagged Pointers with Versioning — In this post, we explain how to solve the ABA problem by pairing pointers with version numbers.
  2. Part 2: Epoch-Based Reclamation — In the next post, we’ll demonstrate a solution using epoch-based reclamation.
  3. Part 3: Hazard Pointers — Finally, we will explore a solution using hazard pointers for safe memory reclamation.

In this first post, our focus is on Tagged Pointers with Versioning.

👨‍🔬 What Is the ABA Problem?

The ABA problem arises when a thread reads a shared variable (value A), which is preempted while it holds that outdated value. In the meantime, another thread changes that shared variable from A to B and then back to A. Because the first thread’s CAS operation only checks for A, it succeeds, failing to detect that B ever happened. This can lead to data corruption because the underlying object at address A may no longer be the same object as before.

Here is a detailed example of the ABA problem:

🏷️ Tagged Pointers with Versioning

👨‍🏫 How It Works

Each pointer is “tagged” with a version number. On every update the version is incremented, so that even if the same memory address is reused, a stale pointer (with an old version) will be detected by the CAS operation.

Note: This implementation requires Nightly Rust because it uses the integer_atomics feature.

🦀 Rust Implementation

Below is a fully functional lock-free stack implementation that leverages tagged pointers to mitigate the ABA problem. Comprehensive inline comments clarify each component of the code.

Read the full blog post here:
https://minikin.me/blog/solving-the-aba-problem-in-rust-tagged-pointers

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Oleksandr Prokhorenko
Oleksandr Prokhorenko

Written by Oleksandr Prokhorenko

Software engineer & occasional sound producer

No responses yet

Write a response