Rust Atomics and Locks by Mara Bos
Mara Bos's "Rust Atomics and Locks," published by O'Reilly in January 2023, is a 250-page guide on low-level concurrency in Rust, covering atomics, mutexes, and memory ordering for all skill levels.
Read original articleMara Bos's book "Rust Atomics and Locks," published by O'Reilly in January 2023, serves as a comprehensive guide to low-level concurrency in Rust programming. Spanning 250 pages, the book addresses the complexities of implementing concurrent data structures and the common pitfalls associated with memory ordering bugs. It is designed for Rust programmers at all levels, providing insights into atomics, mutexes, condition variables, and the interaction between Rust's memory model, processors, and operating systems. The book covers essential topics such as the workings of Rust's type system in concurrency, practical atomic operations on Intel and ARM processors, and the implementation of locking mechanisms with OS support. Readers will learn to write correct concurrent code and build their own synchronization primitives. The book includes code examples available on GitHub, enhancing the learning experience.
- "Rust Atomics and Locks" was released in January 2023 by O'Reilly.
- The book focuses on low-level concurrency in Rust, addressing atomics, mutexes, and memory ordering.
- It is suitable for Rust programmers of all skill levels and includes practical examples.
- Code examples from the book can be accessed on GitHub.
- The book emphasizes the importance of understanding Rust's memory model and its interaction with processors and operating systems.
Related
Atomic Operations Composition in Go
The article discusses atomic operations composition in Go, crucial for predictable results in concurrent programming without locks. Examples show both reliable and unpredictable outcomes, cautioning about atomics' limitations compared to mutexes.
Properly Testing Concurrent Data Structures
The article explores testing concurrent data structures using the Rust library loom. It demonstrates creating property tests with managed threads to simulate concurrent behavior, emphasizing synchronization challenges and design considerations.
Atomicless Per-Core Concurrency
The article explores atomicless concurrency for efficient allocator design, transitioning from per-thread to per-CPU structures on Linux. It details implementing CPU-local data structures using restartable sequences and rseq syscall, addressing challenges in Rust.
Rust for Rustaceans
"Rust for Rustaceans" by Jon Gjengset is a book for developers with basic Rust knowledge, focusing on advanced topics, codebases, and programming challenges, praised for clarity but noted for its density.
Show HN: I'm writing an interactive book on Go concurrency
Anton Zhiyanov's interactive book teaches Go concurrency through 44 practical exercises, covering goroutines, channels, and synchronization. Currently in progress, it aims for completion by late 2024, with online access available.
- Many readers find the book to be an excellent introduction to low-level concurrency concepts, applicable beyond just Rust.
- Some users express concerns about specific implementation details, such as the issues with Windows SRWLocks mentioned in the comments.
- Readers appreciate the clarity and comprehensiveness of the book, noting its usefulness for both beginners and experienced programmers.
- There is a desire for more practical examples and project suggestions that utilize the concepts discussed in the book.
- Several comments highlight the book's value for those working with async Rust and its clear explanations of key primitives.
Since then, two important things happened.
1. On Windows 8 and beyond Rust moved to WaitOnAddress with an API similar to the futex on several other systems.
2. We found out SRWLocks have a significant (arguably fatal, but depending on your use case it may seem irrelevant) difference between how they actually work and what Microsoft's API said about them. This bug is fixed... in Microsoft's own version control, not in released Windows versions.
Specifically SRWLocks may silently give you a Write Lock, even if you asked only for a Read Lock, in the case where the lock was just released at the moment you asked. If you were expecting other threads to also get a read lock, which would ordinarily be possible - too bad, you've secretly been given the exclusive write lock so read locks are unavailable until you release it.
The actual reason seems to be this: SRWLocks are small (a single pointer, with some low bits stolen to hide metadata) and the authors forgot that they actually know (because it's a different function call) whether you asked for a read or a write lock. Since they didn't have anywhere to store this single bit (read or write) they just assumed they don't know in this edge case where the lock happens to be available immediately, and since they "don't know" they always give you a write lock anyway. Oops.
[Edited to make minor clarifications]
A classic example is a set of bank accounts, atomically transacting with each other. Fine-grained per-account locking is possible, but risks deadlock due to lock ordering inversion. A simple solution is to replace per-account locks with a singleton global lock, covering all accounts. Any transaction must first acquire this lock, and now deadlock is impossible.
But this is an awkward fit for Rust, whose locks want to own the data they protect. What's the best way to express a global lock, enforcing that certain data may only be accessed while the global lock is held?
One thing I found lacking in the book were the examples. It has tons, but all of them are extremely focused on the topic they are illustrating and most feel very contrived. Would anyone here have a suggestion for a small/medium-sized project (weekend sized) which would actually use the patterns discussed in the book?
Usually when you have a textbook, they will have some nice illustration that is tangentially related to the content of the book (like fibonacci spiral for a math book or some chemical reaction for a chemistry book for example). But I suppose that there isn't really such an equivalent unless it's a computer graphics book.
I guess it's also like how every project has to have its own "cutesey" mascot.
Related
Atomic Operations Composition in Go
The article discusses atomic operations composition in Go, crucial for predictable results in concurrent programming without locks. Examples show both reliable and unpredictable outcomes, cautioning about atomics' limitations compared to mutexes.
Properly Testing Concurrent Data Structures
The article explores testing concurrent data structures using the Rust library loom. It demonstrates creating property tests with managed threads to simulate concurrent behavior, emphasizing synchronization challenges and design considerations.
Atomicless Per-Core Concurrency
The article explores atomicless concurrency for efficient allocator design, transitioning from per-thread to per-CPU structures on Linux. It details implementing CPU-local data structures using restartable sequences and rseq syscall, addressing challenges in Rust.
Rust for Rustaceans
"Rust for Rustaceans" by Jon Gjengset is a book for developers with basic Rust knowledge, focusing on advanced topics, codebases, and programming challenges, praised for clarity but noted for its density.
Show HN: I'm writing an interactive book on Go concurrency
Anton Zhiyanov's interactive book teaches Go concurrency through 44 practical exercises, covering goroutines, channels, and synchronization. Currently in progress, it aims for completion by late 2024, with online access available.