concurrency_primitives.txt (2929B)
1 Concurrency Primitives 2 3 Concurrency primitives in Rust are the fundamental building blocks that 4 allow multiple parts of a program to run simultaneously — safely and without 5 data races. They are the low-level tools you use to coordinate concurrent tasks, 6 share data between threads, and synchronize execution. 7 8 ## Why Rust Is Special Here 9 10 Rust's ownership and type system enforce concurrency safety at compile time, 11 not at runtime. Many bugs that would silently corrupt data in other languages 12 become compile errors in Rust, which is why Rust calls its approach "fearless 13 concurrency". 14 [doc.rust-lang](https://doc.rust-lang.org/book/ch16-00-concurrency.html) 15 16 ## The Core Primitives 17 18 - Threads — the most basic primitive; 19 independent paths of execution that run concurrently, letting you exploit 20 multi-core processors 21 [earthly](https://earthly.dev/blog/rust-concurrency-patterns-parallel-programming/) 22 23 - Channels (mpsc) — typed message-passing pipes with a sender and receiver 24 handle; one thread sends data, another receives it, avoiding shared memory 25 entirely [news.ycombinator](https://news.ycombinator.com/item?id=7851274) 26 27 - Mutex (Mutex<T>) — short for _mutual 28 exclusion_; only one thread can access the protected data at a time, 29 preventing data races on shared state 30 [earthly](https://earthly.dev/blog/rust-concurrency-patterns-parallel-programming/) 31 32 - Arc (Arc<T>) — _Atomic Reference Counting_; 33 lets multiple threads share ownership of a value safely 34 [doc.rust-lang](https://doc.rust-lang.org/book/ch16-03-shared-state.html) 35 36 - RwLock<T> — like a Mutex, but allows 37 many simultaneous readers or one exclusive writer 38 39 - Atomic types — low-level primitives (e.g., 40 `AtomicUsize`) for lock-free, thread-safe operations on simple values 41 [web.mit](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/concurrency.html) 42 43 ## Key Traits: `Send` and `Sync` 44 45 Rust enforces concurrency rules through two marker traits: 46 [web.mit](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/concurrency.html) 47 48 - `Send` — a type can be transferred (moved) to another thread 49 - `Sync` — a type can be safely _referenced_ from multiple threads 50 simultaneously 51 52 These traits are automatically implemented by the compiler where safe, and 53 withheld where they aren't — so you can't accidentally send a non-thread-safe 54 type across a thread boundary. 55 [google.github](https://google.github.io/comprehensive-rust/concurrency/welcome.html) 56 57 ## A Simple Mental Model 58 59 Think of concurrency primitives as traffic rules for threads. Channels say 60 _"pass the data by handing it off"_, while `Mutex`/`Arc` say _"share the data, 61 but take turns"_. Rust's compiler acts as the traffic enforcer, rejecting unsafe 62 patterns before your code ever runs. 63 [dzone](https://dzone.com/articles/concurrency-in-rust-safe-and-efficient-code)