Archive

1046 posts · Page 77 of 88. ← Blog

Pratik Dhanave · ·8 min read

Message Passing with Channels

There are two great philosophies of concurrency: share memory (with locks, as the previous posts covered) or share nothing and communicate by passing messages. The message-passing school has a famous slogan — "do not communicate by sharing memory; instead, share memory by communicating" — and Rust supports it fully with channels. Instead of multiple threads carefully locking shared state, ownership of data is transferred from one thread to another through a channel, and Rust's ownership system makes that transfer clean and safe.

There are two great philosophies of concurrency: share memory (with locks) or share nothing and communicate by passing messages. Rust supports channels fully — and its ownership system makes message passing especially natural, because sending data through a channel *is* transferring ownership.

Pratik Dhanave · ·8 min read

Send and Sync: The Traits Behind Fearless Concurrency

How does the Rust compiler actually know that an `Arc<Mutex<T>>` is safe to share across threads but an `Rc<T>` isn't? The answer is two of the most elegant ideas in Rust: a pair of marker traits, `Send` and `Sync`, that encode thread-safety directly into the type system. They're rarely written by hand and often invisible, yet they're the machinery that makes fearless concurrency work — the compiler reasons about thread-safety by checking these traits, automatically, at compile time.

How does the compiler know an Arc<Mutex<T>> is safe to share across threads but an Rc<T> isn't? The answer is two elegant marker traits — Send and Sync — that encode thread-safety directly into the type system. Rarely written by hand and often invisible, they're the machinery that makes fearless concurrency work.

Pratik Dhanave · ·8 min read

Shared State: Arc and Mutex

Moving data into a single thread is safe but limiting — sometimes multiple threads genuinely need to share and mutate the same data. This is exactly where data races live in other languages, and where Rust's guarantees shine brightest. The answer is a pair of types, `Arc` and `Mutex`, that let you share mutable state across threads — and the compiler will refuse to compile code that shares it unsafely. You literally cannot forget the lock, because the data lives inside it.

Sometimes multiple threads genuinely need to share and mutate the same data — exactly where data races live in other languages, and where Rust's guarantees shine brightest. The answer is Arc and Mutex, which let you share mutable state across threads while the compiler refuses to compile unsafe sharing. You literally cannot forget the lock.

Pratik Dhanave · ·6 min read

Threads and Fearless Concurrency

Rust's boldest promise is "fearless concurrency" — the claim that you can write multithreaded code and have the compiler guarantee, at compile time, that you have no data races. Coming from languages where concurrency bugs are a dark art of subtle, intermittent horror, this sounds too good to be true. It isn't: the same ownership and borrowing rules that give Rust memory safety extend naturally to threads. This module explores concurrency, starting with the basics — spawning threads and moving data into them.

Rust's boldest promise is fearless concurrency — write multithreaded code and have the compiler guarantee, at compile time, that you have no data races. It isn't too good to be true: the same ownership and borrowing rules that give memory safety extend naturally to threads. Module 3 begins with the basics — spawning threads and moving data into them.