concurrency_primitives.md (3122B)
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**](/operating_systems/thread.md) — 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 - **Channels (`mpsc`)** — typed message-passing pipes with a sender and receiver 23 handle; one thread sends data, another receives it, avoiding shared memory 24 entirely [news.ycombinator](https://news.ycombinator.com/item?id=7851274) 25 - [**Mutex (`Mutex<T>`)**](/memory_safety/mutex.md) — short for _mutual 26 exclusion_; only one thread can access the protected data at a time, 27 preventing data races on shared state 28 [earthly](https://earthly.dev/blog/rust-concurrency-patterns-parallel-programming/) 29 - [**Arc (`Arc<T>`)**](/memory_safety/arc.md) — _Atomic Reference Counting_; 30 lets multiple threads share ownership of a value safely 31 [doc.rust-lang](https://doc.rust-lang.org/book/ch16-03-shared-state.html) 32 - [**`RwLock<T>`**](/memory_safety/rwlock_pattern.md) — like a Mutex, but allows 33 many simultaneous readers or one exclusive writer 34 - [**Atomic types**](/memory_safety/atomic.md) — low-level primitives (e.g., 35 `AtomicUsize`) for lock-free, thread-safe operations on simple values 36 [web.mit](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/concurrency.html) 37 38 ## Key Traits: `Send` and `Sync` 39 40 Rust enforces concurrency rules through two marker traits: 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 - **`Send`** — a type can be transferred (moved) to another thread 44 - **`Sync`** — a type can be safely _referenced_ from multiple threads 45 simultaneously 46 47 These traits are automatically implemented by the compiler where safe, and 48 withheld where they aren't — so you can't accidentally send a non-thread-safe 49 type across a thread boundary. 50 [google.github](https://google.github.io/comprehensive-rust/concurrency/welcome.html) 51 52 ## A Simple Mental Model 53 54 Think of concurrency primitives as traffic rules for threads. Channels say 55 _"pass the data by handing it off"_, while `Mutex`/`Arc` say _"share the data, 56 but take turns"_. Rust's compiler acts as the traffic enforcer, rejecting unsafe 57 patterns before your code ever runs. 58 [dzone](https://dzone.com/articles/concurrency-in-rust-safe-and-efficient-code)