Interior mutability¶
Category: Safety in languages · Status: stub
One line: Changing data behind a shared reference through a type that enforces the rules itself — Cell and RefCell within one thread, Mutex and atomics across threads.
How it connects¶
flowchart LR
n_atomic_variable["Atomic variable"]
n_interior_mutability["Interior mutability"]
n_mutex["Mutex"]
n_interior_mutability -->|uses| n_atomic_variable
n_interior_mutability -->|uses| n_mutex
classDef center stroke-width:3px
class n_interior_mutability center
classDef outside stroke-dasharray: 4 3
class n_atomic_variable,n_mutex outside
- Is built on: Atomic variable, Mutex
In each language¶
| Rust | std::cell ↗: Cell, RefCell and OnceCell are single-threaded and not Sync; across threads use Mutex, RwLock or atomics |
| C++ | The mutable ↗ specifier lets a member change inside a const object — typically a mutex, as in the page's rule that mutable and mutex go together |
Where to read more¶
- In a sibling library: Rust: Interior mutability ↗
- In the books: Programming Concurrency on the JVM, Venkat Subramaniam — ch. 5, 'Taming Shared Mutability'
- In the books: Rust Atomics and Locks, Mara Bos — ch. 1, 'Basics of Rust Concurrency' → 'Interior Mutability'
- In the books: Haskell Cookbook, Yogesh Sajanikar — ch. 12, 'Concurrent and Distributed Programming in Haskell' → 'Working with IORef'
- Notes: interior mutability - rust ↗
- Reference: The Rust Reference: Interior mutability ↗