atomic.md (3300B)
1 # Atomic 2 3 In software engineering, **atomic** comes from the Greek word _atomos_, meaning 4 **indivisible** — and that's exactly what it means in code. An atomic operation 5 is one that either fully completes or hasn't happened at all; it can never be 6 observed in a half-finished state by another thread.[^3][^5] 7 8 ## Why It Matters in Concurrency 9 10 Without atomicity, even a simple operation like `x += 1` compiles down to 11 multiple CPU instructions (load, add, store), which can be interrupted mid-way 12 by another thread. This creates **race conditions** where two threads read the 13 same value, both increment it, and one update is silently lost. Atomic 14 operations solve this by guaranteeing the entire read-modify-write cycle happens 15 as one uninterruptible unit.[^5][^6] 16 17 On a multi-core CPU, when a core begins an atomic operation, it pauses memory 18 access from other cores for that location until the operation completes.[^4] 19 20 ## Atomics in Rust 21 22 Rust exposes atomic operations through the `std::sync::atomic` module. The types 23 all start with `Atomic`, for example:[^15] 24 25 - `AtomicBool` — atomic boolean 26 - `AtomicI32`, `AtomicUsize` — atomic integers 27 - `AtomicPtr` — atomic pointer 28 29 Common operations on these types include:[^3] 30 31 - **`load`** — reads the value atomically 32 - **`store`** — writes a value atomically 33 - **`fetch_add`** — atomically increments and returns the old value 34 - **`compare_exchange`** — atomically checks if a value matches an expectation, 35 and only then replaces it 36 37 ## Memory Ordering 38 39 Every atomic operation in Rust requires an `Ordering` argument (e.g., `Relaxed`, 40 `Acquire`, `Release`, `SeqCst`). This controls how the CPU and compiler may 41 reorder instructions around the atomic operation relative to other memory 42 accesses — a subtle but critical detail for correctness in concurrent code. Rust 43 inherits this memory model from C++20.[^9][^10] 44 45 ## Role in the Ecosystem 46 47 Atomic types are the **lowest-level building block** for concurrency in Rust. 48 Higher-level primitives like `Mutex` and `RwLock` are themselves implemented 49 using atomic operations under the hood.[^3] 50 <span style="display:none">[^1][^11][^12][^13][^14][^2][^7][^8]</span> 51 52 <div align="center">⁂</div> 53 54 [^1]: https://doc.rust-lang.org/std/sync/atomic/ 55 56 [^2]: https://doc.rust-lang.org/nomicon/atomics.html 57 58 [^3]: https://marabos.nl/atomics/atomics.html 59 60 [^4]: https://leapcell.io/blog/rust-atomics-explained 61 62 [^5]: https://whenderson.dev/blog/implementing-atomics-in-rust/ 63 64 [^6]: https://stackoverflow.com/questions/53587866/what-is-the-difference-between-this-atomic-rust-code-and-its-non-atomic-coun 65 66 [^7]: https://www.reddit.com/r/rust/comments/hskm11/having_a_hard_time_understanding_atomic/ 67 68 [^8]: http://blog.rustbr.org/entendendo-atomicos/ 69 70 [^9]: https://cfsamsonbooks.gitbook.io/explaining-atomics-in-rust 71 72 [^10]: https://www.youtube.com/watch?v=rMGWeSjctlY 73 74 [^11]: https://blog.rustbr.org/en/understanding-atomics/ 75 76 [^12]: https://www.reddit.com/r/rust/comments/1ksqo9i/mastering_rust_atomic_types_a_guide_to_safe/ 77 78 [^13]: https://dev.to/leapcell/rust-concurrency-atomic-explained-58cl 79 80 [^14]: https://rust-lang.guide/guide/learn-async-rust/rust-atomics-and-locs.html 81 82 [^15]: https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/std/sync/atomic/index.html