Rust atomics — Atomic types and memory Ordering

Contents

  1. 1. Mutex vs Atomic
  2. 2. First look at Atomic
  3. 3. Instruction reordering
  4. 4. Ordering
  5. 5. Seeing the difference in visibility
  6. 6. fence
  7. 7. Lazy initialization

Mutex and Condvar are higher-level ways to share data between threads in Rust. Lower down the stack you have atomics — hardware-backed operations on individual locations.

Below: Atomic* types and Ordering, mostly through runnable examples.

Why bother with atomics if Mutex already exists? Compare the two.

Mutex vs Atomic

1. How they touch data

Mutex enforces exclusive access: many threads may try to write, but only one holds the lock at a time. Everyone else waits until it is released.

Atomic operations are indivisible at the machine level — built on CPU primitives like load, store, compare-and-swap (CAS), and fetch-add. Each operation completes fully or not at all; there is no halfway state where another thread can tear the update apart.

So atomics lean on non-splittable instructions for lock-free access to a single memory location.

2. Critical sections

With Mutex, the protected region sits between lock() and unlock().

An atomic only guarantees atomicity for that one address. If your critical section involves more state, you typically combine load/store/CAS with a spin loop — the lower-level cousin of a mutex. That shows up when you care about every nanosecond.

Pseudo-code for a flag-based critical section (the Ordering section below makes this concrete):

1
2
3
4
5
6
7
8
thread 1: 
// set flag when condition is ready
store/CAS flag: false->true
thread 2:
// spin until flag is set (like blocking on a lock)
while load flag == false {};
// critical section
...

First look at Atomic

A minimal two-thread example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::{
sync::atomic::{AtomicBool, Ordering},
thread,
};
static FLAG: AtomicBool = AtomicBool::new(false);

fn main() {
let a = thread::spawn(|| {
FLAG.store(true, Ordering::Relaxed);
});
let b = thread::spawn(|| {
if FLAG.load(Ordering::Relaxed) {
println!("Relaxed: Flag is set!");
}
});
a.join().unwrap();
b.join().unwrap();
}

Two threads store and load FLAG. The reader prints when it sees true.

You might run this and not see the print. Scheduling could run load before store, and that alone would explain a miss — but that is not the whole story.

The rest involves instruction reordering and atomic Ordering.

Instruction reordering

1
2
3
4
5
fn f(a: &mut i32, b: &mut i32) {
*a += 1;
*b += 1;
*a += 1;
}

You write one sequence; the CPU or compiler may execute an equivalent faster one:

1
2
3
4
fn f(a: &mut i32, b: &mut i32) {
*a += 2;
*b += 1;
}

Why?

As long as single-thread semantics stay the same, instructions may be reordered for speed — execution order is not guaranteed to match source order.

On one thread that is often fine. Across threads, even two atomics in the same thread can be reordered relative to each other, so “I used atomics” does not automatically mean “I fixed the order other threads observe.”

Example — steps 1 and 2 in thread b are independent, so they may swap:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::{
sync::atomic::{AtomicI32, Ordering},
thread,
};

static X: AtomicI32 = AtomicI32::new(0);
static Y: AtomicI32 = AtomicI32::new(0);

fn main() {
let a = thread::spawn(|| {
let x = X.load(Ordering::Relaxed);
Y.store(x, Ordering::Relaxed);
});
let b = thread::spawn(|| {
// 1
let y = Y.load(Ordering::Relaxed);
// 2
X.store(42, Ordering::Relaxed);
});
a.join().unwrap();
b.join().unwrap();
assert_eq!(X.load(Ordering::Relaxed), 42);
// possible: b's 1 and 2 reordered to 2 then 1
assert_eq!(Y.load(Ordering::Relaxed), 42);
}

Ordering constrains when an atomic’s effect becomes visible to other threads, so concurrent updates are more predictable.

Ordering

Rust’s Ordering aligns with C++20 memory order for the most part. The variants:

  • Relaxed

The weakest guarantee: the operation itself is atomic, but there is no ordering promise for how this store/load relates to other memory accesses on other threads (as in the reordering example).

  • Acquire

For loads. No other reads or writes on this thread may be reordered before this load. Release stores to the same location from other threads that happened before you see the loaded value are visible.

  • Release

For stores. No other reads or writes on this thread may be reordered after this store. Threads that later do an Acquire load on the same location will see this write.

So pairing Release (writer) with Acquire (reader) is the usual handoff: “data is ready” on the flag, payload writes before the Release, reads after the Acquire.

See comments in the code for where reordering is forbidden:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::{
sync::atomic::{AtomicBool, Ordering},
thread,
};
fn main() {
// For stricter exploration, consider `loom`
for _ in 0..100000 {
acquire_release()
}
}

fn acquire_release() {
static FLAG: AtomicBool = AtomicBool::new(false);
static mut DATA: u64 = 0;
let a = thread::spawn(|| {
unsafe { DATA = 42 };
FLAG.store(true, Ordering::Release);
// no reads/writes reordered after this store
});
let b = thread::spawn(|| {
// no reads/writes reordered before this load
while !FLAG.load(Ordering::Acquire) {}
assert_eq!(unsafe { DATA }, 42);
});

a.join().unwrap();
b.join().unwrap();
}
  • AcqRel

For read-modify-write ops: Acquire semantics on the read side, Release on the write side.

  • SeqCst

Acquire/Release plus a single total order of all SeqCst atomics that every thread agrees on. Interleavings can still vary run to run, but once a particular execution has happened, there is no alternate consistent ordering of those SeqCst operations — as if they ran on one global timeline.

Strictness ramp: Relaxed → Acquire/Release → SeqCst, with cost usually going up as well.

Building a critical section with explicit orderings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
fn main() {
let lock = Arc::new(AtomicBool::new(false));
let lock_clone_read = lock.clone();
let lock_clone_store = lock.clone();
thread::spawn(move || {
lock.store(true, Ordering::SeqCst);
// critical section work
});

while !lock_clone_read.load(Ordering::Acquire) {}

println!("Critical section!");

lock_clone_store.store(false, Ordering::Release);
}

Seeing the difference in visibility

Does the ordering strength matter in practice? Try Relaxed first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::{
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Arc,
},
thread,
};

static S: AtomicU64 = AtomicU64::new(0);
fn relaxed() {
let a = Arc::new(AtomicBool::new(false));
let b = Arc::new(AtomicBool::new(false));
let a_clone = a.clone();
let b_clone = b.clone();

let t1 = thread::spawn(move || {
a.store(true, Ordering::Relaxed);
if !b.load(Ordering::Relaxed) {
S.fetch_add(1, Ordering::Relaxed);
}
});

let t2 = thread::spawn(move || {
b_clone.store(true, Ordering::Relaxed);
if !a_clone.load(Ordering::Relaxed) {
S.fetch_add(1, Ordering::Relaxed);
}
});

t1.join().unwrap();
t2.join().unwrap();
}

fn main() {
let cnt = 100000;
for _ in 0..cnt {
relaxed();
}
// result can be greater than 100000
let s = S.load(Ordering::SeqCst);
println!("s: {}", s);
}

S often ends up above 100000. Why?

Both AtomicBools start false. Each thread stores its flag true with Relaxed, then loads the other. If the load still sees false, it increments S.

Possibilities:

  1. Other thread’s instructions reordered

    • Store happens after the load; increment (under Relaxed).
  2. No reordering

    • Other thread has not stored yet → increment.
    • Other thread stored but load does not see it yet → increment (Relaxed).
    • Load sees the store → no increment.

Reordering plus weak visibility both push S up. Run 100000 iterations and overshooting 100000 is easy with Relaxed.

Switch the bool ops to SeqCst: no reordering like above, and a global order on those atomics. If the other thread’s store is before your load in that order, you see it — you do not get both threads thinking the partner is still false. S tops out at 100000.

1
2
3
4
5
6
7
8
9
10
11
12
13
let t1 = thread::spawn(move || {
a.store(true, Ordering::SeqCst);
if !b.load(Ordering::SeqCst) {
S.fetch_add(1, Ordering::Relaxed);
}
});

let t2 = thread::spawn(move || {
b_clone.store(true, Ordering::SeqCst);
if !a_clone.load(Ordering::SeqCst) {
S.fetch_add(1, Ordering::Relaxed);
}
});

fence

Ordering applies per atomic op. fence adds a memory barrier across multiple atomics (and surrounding accesses), blocking certain reorders — another way to stitch a critical section together.

1
2
3
4
a.store(1, Release);
// equivalent pattern:
fence(Release);
a.store(1, Relaxed);
1
2
3
4
a.load(Acquire);
// equivalent pattern:
a.load(Relaxed);
fence(Acquire);

Fences help when several locations need a coordinated visibility story. You can mix Relaxed ops with a fence instead of tagging every operation AcqRel/SeqCst.

Toy mutex built from CAS + fences:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::sync::atomic::AtomicBool;
use std::sync::atomic::fence;
use std::sync::atomic::Ordering;

pub struct Mutex {
flag: AtomicBool,
}

impl Mutex {
pub fn new() -> Mutex {
Mutex {
flag: AtomicBool::new(false),
}
}

pub fn lock(&self) {
// Relaxed CAS + Acquire fence to sync with prior Release unlock
while self
.flag
.compare_exchange_weak(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_err()
{}
fence(Ordering::Acquire);
}

pub fn unlock(&self) {
self.flag.store(false, Ordering::Release);
}
}

Lazy initialization

Atomics often back lazy init of shared static-like data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::{
sync::atomic::{AtomicPtr, Ordering},
thread,
};

struct Data {}
fn generate_data() -> Data {
Data {}
}
fn get_data() -> &'static Data {
static PTR: AtomicPtr<Data> = AtomicPtr::new(std::ptr::null_mut());

let mut p = PTR.load(Ordering::Acquire);

if p.is_null() {
p = Box::into_raw(Box::new(generate_data()));
if let Err(e) = PTR.compare_exchange(
std::ptr::null_mut(),
p,
Ordering::Release,
Ordering::Acquire,
) {
// Safety: p comes from Box::into_raw right above,
// and wasn't shared with any other thread.
drop(unsafe { Box::from_raw(p) });
p = e;
}
}

// Safety: p is not null and points to a properly initialized value.
unsafe { &*p }
}
fn main() {
let t1 = thread::spawn(|| get_data());
let t2 = thread::spawn(|| get_data());
let (ret1, ret2) = (t1.join().unwrap(), t2.join().unwrap());
assert_eq!(ret1 as *const _, ret2 as *const _);
}

In real code you rarely hand-roll this — crates like once_cell cover the pattern.


Atomics are the low-level layer. Ordering ties atomic results to what other threads can observe and pushes back on compiler/CPU reordering. You might never ship your own lock-free structure, but knowing this makes library code and the ordering names far easier to read.

Solid follow-ups:

Questions? Leave a comment below or email: newbvirgil@gmail.com