Week 9: Concurrency & Parallelism (Intermediate)
Overview
Week 9 introduces concurrent and parallel programming in Rust. You’ll learn how Rust’s ownership and type system guarantees thread safety, eliminating entire classes of bugs common in other languages. This week covers both thread-based concurrency and message passing, providing a strong foundation for high-performance, safe concurrent code.
Day 1-4: Threading and Shared State
Topics
- Creating threads with
std::thread
- Thread lifecycle management
- Join handles and blocking
- Thread-local storage
- Shared state concurrency:
- Mutex and interior mutability
- RwLock for multiple readers
- Arc for thread-safe reference counting
- Poisoning and recovery
- Thread safety traits:
- Send and Sync
- Auto traits and marker traits
- Thread bounds on generic types
- Atomic types in
std::sync::atomic
- Memory ordering
- Barriers and thread coordination
- Thread pools and work stealing
Resources
- Rust Book Ch 16.1-16.3
- Rust Atomics and Locks (free online book)
- Fearless Concurrency
- Rust Concurrency Explained
- Thread documentation
- Sync documentation
Use Cases
- Multi-threaded data processing
- Parallelizing CPU-intensive tasks
- Managing shared resources safely
- Building thread-safe data structures
- Implementing concurrent algorithms
- Utilizing multi-core processors effectively
Day 5-7: Message Passing and Channels
Topics
- Message passing concurrency
- Channels in
std::sync::mpsc
:- Single-producer, multi-consumer
- Bounded vs unbounded channels
- Synchronous vs asynchronous channels
- Channel ownership and borrowing
- Producer and consumer patterns
- Fan-out and fan-in designs
- Implementing worker pools
- Error handling in concurrent contexts
- Actor pattern basics
- Synchronization primitives:
- Barrier
- Condvar (condition variables)
- Semaphore (from third-party crates)
- Comparing with JavaScript’s approach (Workers, Promises)
Resources
- Rust Book Ch 16.2
- MPSC documentation
- Crossbeam Channels
- Rust Concurrency Patterns
- Actor Systems in Rust
Use Cases
- Event-driven architectures
- Pipeline processing models
- Task distribution systems
- Producer-consumer scenarios
- Backpressure handling
- Building responsive applications
Exercises
- Create a multi-threaded application that processes data in parallel
- Implement a thread-safe cache with Mutex and Arc
- Build a pipeline using channels for data processing
- Create a thread pool with work distribution via channels
- Develop a simple map-reduce pattern using threads
- Implement a concurrent counter with atomic types
Advanced Challenges
- Build a multi-threaded web crawler
- Implement a thread-safe data structure (e.g., concurrent HashMap)
- Create a work-stealing thread pool
- Develop an actor system for message passing
- Build a parallel image processing tool
Next Steps
After completing Week 9, you’ll understand how to write safe, concurrent code in Rust. Week 10 will build on this foundation by introducing asynchronous programming with Rust’s async/await system.