Details in Atomic Acquire/Release semantics

⚓ Rust    📅 2025-07-14    👤 surdeus    👁️ 2      

surdeus

Reading the docs here Atomics - The Rustonomicon

When thread A releases a location in memory and then thread B subsequently acquires the same location in memory, causality is established. Every write (including non-atomic and relaxed atomic writes) that **happened before** A's release will be observed by B after its acquisition. However no causality is established with any other threads. Similarly, no causality is established if A and B access different locations in memory.

Here what does "happened before" A for a non-atomic write precisely mean?
Will any write appearing before in the code qualify, or is some logical causality required?

// thread 1
let my_ptr: *mut usize = //...
std::ptr::write(my_ptr, 5); // C
shared_atomic_ptr.store(my_ptr, Ordering::Release);  // A
// thread 2
let my_ptr: *const  = shared_atomic_ptr.load(my_ptr, Ordering::Acquire); // B
let val = unsafe { std::ptr::read(my_ptr) };

Assuming that in B, we read the value from A's write;
are we guaranteed that val will be 5, or is it possible for the write C to be reordered after A?

4 posts - 3 participants

Read full topic

🏷️ rust_feed