# Any guideline on which atomic ordering to choose?

**URL:** <https://discourse.julialang.org/t/any-guideline-on-which-atomic-ordering-to-choose/72055>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [November 25, 2021, 10:56am UTC](https://discourse.julialang.org/t/any-guideline-on-which-atomic-ordering-to-choose/72055 "2021-11-25T10:56:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)\
**Post date:** [November 25, 2021, 10:56am UTC](https://discourse.julialang.org/t/any-guideline-on-which-atomic-ordering-to-choose/72055/1 "2021-11-25T10:56:14Z")

</div>

In the upcoming `Julia@v1.7` [Per-field atomics](https://docs.julialang.org/en/v1.7.0-rc3/manual/multi-threading/#man-atomics), it mentions several atomic orderings: ` (:monotonic, :acquire, :release, :acquire_release, or :sequentially_consistent)`. Do you have any general suggestions for which one to use?

---

<div class="post-metadata">

**Author:** ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)\
**Post date:** [November 25, 2021, 11:14am UTC](https://discourse.julialang.org/t/any-guideline-on-which-atomic-ordering-to-choose/72055/2 "2021-11-25T11:14:44Z")

</div>

> The basic premise is: do not use anything other than `std::memory_order_seq_cst` (the default) unless (a) you really **really** know what you are doing, and can **prove** that the relaxed usage is safe in all cases, and (b) your profiler demonstrates that the data structure and operations you are intending to use the relaxed orderings with are a bottleneck.

> <https://stackoverflow.com/questions/9553591/c-stdatomic-what-is-stdmemory-order-and-how-to-use-them>

* * *

> Specific details of the memory model and other details of the design are written in the [Julia Atomics Manifesto](https://gist.github.com/vtjnash/11b0031f2e2a66c9c24d33e810b34ec0), which will later be published formally.

### Julia Atomics Manifesto

> These memory orderings can be any of the following symbols. More precise definitions can be found elsewhere (in particular, at [https://llvm.org/docs/Atomics.html#atomic-orderings](https://llvm.org/docs/Atomics.html#atomic-orderings)), so here we give only the particulars of how they will be mapped inside the Julia runtime.
> 
> - `not_atomic`: single-threaded usage only, or inside a lock. This is the default behavior for most operations. This cannot be used to down-grade an atomic write, but simply names the default parameterization for a non-atomic access. And when applied to an atomic field, it’ll be automatically upgraded to a `monotonic` read.
> - `monotonic`: This ensures atomicity of the operation, but otherwise does not ensure the code makes forward progress (do not use in a loop), but only ensures that the operations are seen monotonically making progress within a thread.
> - `acquire`: This is an error to specify on a store.
> - `release`: This is an error to specify on a load.
> - `acquire_release`: This is an error to specify on an operation that doesn’t both read and write memory (so it’s an error to specify on a load or store, and the release will be ignored for a `replacefield!` failure).
> - `sequentially_consistent`: Sequential consistency is the default behavior for `@atomic` when the ordering is not specified.
> 
> The specification and constraints on how these apply to individual operations is particularly nicely described here: [https://doc.rust-lang.org/std/sync/atomic/struct.AtomicI8.html](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicI8.html)

> <https://gist.github.com/vtjnash/11b0031f2e2a66c9c24d33e810b34ec0>

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [July 22, 2023, 2:33pm UTC](https://discourse.julialang.org/t/any-guideline-on-which-atomic-ordering-to-choose/72055/3 "2023-07-22T14:33:37Z")

</div>

This is useful information that should probably be in the official docs. At least there was no discussion of these settings here: [Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/base/multi-threading/#Base.@atomic)
