When should one use `Relaxed` memory ordering

โš“ Rust    ๐Ÿ“… 2026-02-18    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 7      

surdeus

When I first started learning about atomics, I used to think it was fine to blindly use Relaxed memory ordering if only a single variable was involved. But after thinking about it more, I feel thatโ€™s not actually true...it also depends on what I want to achieve with that atomic variable.
For example, if we need some information (e.g., โ€œplease stopโ€) to reach another thread, Relaxed ordering might not be a good choice, right? Since there would be no happens before relationship between the threads (assuming all the joins happen after spawning all the threads), in that case we might want to use Release and Acquire.

The only guarantee we sort of get when using Relaxed is that, after all the atomic operations on a variable are done, we will see what we were expecting, correct?

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed