Blog – #rust

Rust's Mutex, Atomics and UnsafeCell – Spooky Action at a Distance?

A defining feature of Rust is its concept of aliasing ⊕ mutability. This rule governs that at any time a value may either have multiple immutable shared references, or a single mutable unique reference, and never both. While this greatly helps in producing fast, efficient and correct code, it can be limiting. To this end, Rust also features types that bend these rules, like Mutex, RwLock, Cell, RefCell, and the ominous UnsafeCell types. In this post we explore how these types interact with Rust's type system and concepts of references and aliasing ⊕ mutability. We do so by looking at how the AtomicUsize and Mutex types are implemented, how violating Rust's assumptions can lead to incorrect optimizations by the compiler, and the surprising global impact of synchronization primitives.

Read more...