CHAPTER 22
Beginner
Concurrency in Rust
Updated: May 18, 2026
5 min read
# CHAPTER 22
Concurrency in Rust
1. Chapter Introduction
In older languages like C++, writing concurrent, multi-threaded code is terrifying. One small mistake leads to "Data Races", where two threads modify the same memory simultaneously, causing random crashes that are nearly impossible to debug. Rust is famous for "Fearless Concurrency". By applying the exact same Ownership and Borrowing rules we learned earlier to threads, the compiler guarantees that Data Races are mathematically impossible. If your multi-threaded code compiles, it is safe.2. Learning Objectives
By the end of this chapter, you will be able to:- Spawn new OS threads.
-
Force the main thread to wait using
join().
-
Move data into threads using the
movekeyword.
- Communicate between threads using Message Passing (Channels).
-
Share memory securely between threads using
MutexandArc.
3. Spawning Threads
You spawn a new thread usingstd::thread::spawn. It takes a Closure containing the code you want the new thread to run.
rust
*If we did not call handle.join(), the main thread would reach the end of the file and instantly shut down the entire program, killing the spawned thread before it could finish!*
4. The move Keyword
If you want a thread to use data from the main scope, you must transfer Ownership of that data into the thread using the move keyword.
rust
5. Message Passing (Channels)
The safest way for threads to communicate is not by sharing memory, but by sending messages to each other. Rust usesmpsc (Multiple Producer, Single Consumer) channels.
rust
6. Shared State (Mutex and Arc)
Sometimes threads *must* share the same piece of memory. To do this safely, we use a Mutex (Mutual Exclusion). A Mutex allows only one thread to access data at any given time.
To share the Mutex across multiple threads, we wrap it in an Arc (Atomic Reference Counted smart pointer—the thread-safe version of Rc).
rust
7. Mini Project: Concurrent Task Runner
*(The Mutex example above represents a complete concurrent architecture!)*8. Common Mistakes
-
Forgetting
join(): Spawning threads but forgetting tojointhem will cause the main thread to exit instantly, making it look like your threads never ran.
-
Using
Rcinstead ofArc: The compiler will scream at you if you try to passRcacross threads.Rcis not thread-safe. You must useArc.
9. Best Practices
-
Prefer Message Passing over Mutexes: Go (Golang) famously says: *"Do not communicate by sharing memory; instead, share memory by communicating."* Using channels (
mpsc) is generally easier to reason about and less prone to Deadlocks than using massiveArc<Mutex<T>>structures.
10. Exercises
-
1.
Create an
mpscchannel.
-
2.
Spawn a thread that sends the number
42through the transmitter.
-
3.
In
main, receive the number and print it.
11. MCQs with Answers
Question 1
What function is used to spawn a new OS thread in Rust?
Question 2
What method must the main thread call to wait for a spawned thread to finish?
Question 3
What keyword forces a thread's closure to take ownership of environmental variables?
Question 4
In Rust's message passing mpsc::channel(), what does mpsc stand for?
Question 5
When a thread uses tx.send(val), what happens to the ownership of val?
Question 6
If multiple threads need to modify the exact same piece of Heap memory, what must you wrap the data in?
Question 7
What does .lock().unwrap() do on a Mutex?
Question 8
When is a Mutex lock released?
Question 9
To share a Mutex among multiple threads, you wrap it in a smart pointer. Which one is Thread-Safe?
12. Interview Questions
- Q: What is a Data Race, and how does Rust's Ownership model completely prevent it?
-
Q: Explain the difference between
RcandArc. Why can'tRcbe used across threads?
13. Summary
Concurrency has historically been the hardest part of systems programming. By enforcing ownership and borrowing rules across thread boundaries, the Rust compiler assumes the burden of thread safety. Whether you choose message passing via channels or shared state viaArc<Mutex<T>>, you can write highly concurrent, multi-core applications with absolute confidence.
14. Next Chapter Recommendation
OS Threads are powerful, but they consume a lot of memory. If you are building a web server that handles 100,000 simultaneous connections, OS threads will crash your machine. In Chapter 23: Async Programming in Rust, we will learn how to handle massive concurrency usingasync/await and the Tokio runtime.