#[non_exhaustive]pub enum Semantics {
Relaxed,
Coupled,
Ordered,
}
Expand description
Memory ordering semantics for atomic operations. Determines how value updates are synchronized between threads
§Explanations
Lock-free programming is not easy to grasp. What’s more, resources explaining Rust’s atomic orderings in depth are pretty sparse. However, this is not really an issue. Atomics in Rust are almost identical to their C++ counterparts, of which there exist abundant explanations
Here are just some of them:
-
Although not meant as an introduction to the release-acquire semantics, this fantastic article by Jeff Preshing definitely provides the much-needed clarification
-
Another great article by Preshing, but this time dedicated entirely to the concept of the release-acquire semantics
-
Memory order from the C++ standards. Way more technical, but has all contracts organized in a single place. Please note that Rust lacks a direct analog to C++’s
memory_order_consume
If you’re still not sure what semantics to use, choose Coupled
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Relaxed
Relaxed
semantics
No synchronization constraints and the best performance
Coupled
Release
- Acquire
coupling semantics
Mild synchronization constraints and fair performance
A read will always see the preceding write (if one exists). Any operations that take place
before the write will also be seen, regardless of their semantics. See the documentation for
Release
and Acquire
A common assumption is that this is how memory operations naturally behave. While it’s true on some platforms (namely, x86 and x86-64), this behavior is not universal. Thus, this is likely the semantics you want to use
Ordered
SeqCst
semantics
Maximum synchronization constraints and the worst performance
All memory operations will appear to be executed in a single, total order. See the
documentation for SeqCst
Implementations§
source§impl Semantics
impl Semantics
sourcepub const fn read_write(&self) -> Ordering
pub const fn read_write(&self) -> Ordering
source§impl Semantics
impl Semantics
sourcepub const fn write(&self) -> Ordering
pub const fn write(&self) -> Ordering
source§impl Semantics
impl Semantics
sourcepub const fn read(&self) -> Ordering
pub const fn read(&self) -> Ordering
Trait Implementations§
source§impl Ord for Semantics
impl Ord for Semantics
source§impl PartialEq for Semantics
impl PartialEq for Semantics
source§impl PartialOrd for Semantics
impl PartialOrd for Semantics
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more