Search code examples
rustatomic

Can I safely use Ordering::Relaxed in single-threaded contexts?


I need (Ref)UnwindSafety, and Cell<bool> doesn't provide it, so I'm using AtomicBool instead.

Is it guaranteed that, in a single-threaded context, updates to one Rc<AtomicBool> using Relaxed (store) ordering are immediately visible on other Rc<AtomicBool>s using Relaxed (load) ordering? (The Rcs point to the same AtomicBool.)


Solution

  • Ordering is about visibility guarantees in the presence of other threads. Memory accesses are always immediately visible within the current thread (barring noalias violations). So even the weakest ordering requirements (Relaxed) are still at least as strong as normal accesses such as those provided by Cell.