Skip to content

⚡ Concurrency & Async

"Fearless Concurrency" - Rust cho phép bạn viết concurrent code mà không sợ data races.

Tổng quan Module


📚 Nội dung Module

1. Fearless Concurrency

  • OS Threads vs Green Threads
  • Message Passing: Cơ chế hoạt động của MPSC Channel
  • Shared State: Arc<Mutex<T>>RwLock
  • Memory Ordering và Atomics

2. Async Runtimes

  • Future Trait: Cơ chế Polling
  • Kiến trúc Tokio Runtime
  • Pinning (Pin<T>): Tại sao futures không được di chuyển

💡 Tại sao Rust là Fearless?

Vấn đềC/C++Java/GoRust
Data RaceLỗi runtimePhát hiện runtimeNgăn chặn compile-time
DeadlockCó thể xảy raCó thể xảy raCó thể (nhưng API an toàn hơn)
Memory corruptionPhổ biếnHiếm (GC)Không thể xảy ra
Thread safetyThủ côngKiểm tra runtimeType system kiểm soát

Rust sử dụng Ownership System để đảm bảo:

  • Không bao giờ có data race (compile-time)
  • SendSync traits kiểm soát thread safety
  • Borrow checker hoạt động xuyên suốt thread boundaries