Explanation of std.builtin.AtomicOrder Enumerations

I’ve now seen it come up in a couple threads (one being mine), but does anyone have a nice summary of what each of the std.builtin.AtomicOrder enumerations mean in practice? For those not familiar, this is the enumerated type used as an argument to builtins like:

For instance, trying the following:

@fence(std.builtin.AtomicOrder.monotonic);

Fails with the following error:

error: atomic ordering must be acquire or stricter

However I’m struggling to grasp what each enumeration actually means when used as an argument to @fence?

This post is potentially a good candidate to get moved to Docs if some nice answers flow in

2 Likes

The AtomicOrder definitions are:

  • unordered: Atomic without memory ordering
  • monotonic: C++ relaxed ordering (weakest ordering)
  • acquire: Acquire lock barrier (when loading)
  • release: Release lock barrier (when storing)
  • acq_rel: Both an Acquire and a Release barrier
  • seq_cst: Sequentially Consistent (strongest ordering)

If you are not sure, use the strongest (and slowest) std.builtin.AtomicOrder.seq_cst.

The definitions came from LLVM: LLVM Atomic Instructions and Concurrency Guide

EDIT:

See: LLVM fence instruction

6 Likes

Awesome thank you!!

1 Like