August 13th, 2024

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 articleLink Icon
AppreciationCuriosityExcitement
Rust Atomics and Locks by Mara Bos

Mara 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.

AI: What people are saying
The comments on Mara Bos's "Rust Atomics and Locks" reflect a range of opinions and insights about the book's content and relevance.
  • 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.
Link Icon 13 comments
By @tialaramex - 9 months
Because this book was written more than a year ago, it spends some time on Windows Slim Reader Writer Locks, SRWLocks which at that time of its writing were how Rust's Mutex and RwLock were implemented on Windows (by the author in fact IIRC).

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]

By @hobofan - 9 months
Even if you are not into Rust, I'd recommend this book if you want to get into low-level and/or embedded programming. It's an exceptionally well written introduction into the most important topics there and ~80% of the book are not specific to Rust (or can be transferred just as well to other languages).
By @nutate - 9 months
I wrote this in a review I believe, but this is one of the most comprehensive introductions to a good 80% of what could be considered a high performance computing education. It's extremely well written, in the weeds, but not lost in them. If you've done heavy atomics and or locks in C or C++ or with Fortran libraries, this will help show you how rust prevents so many footguns at compile time.
By @ridiculous_fish - 9 months
Any thoughts on the best way to express global locks in Rust?

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?

By @ra0x3 - 9 months
If you're into Rust and need a solid, no-fluff intro to atomics and locks, Mara Bos has you covered. It’s straight to the point, helping you nail down concurrency without the usual headache. Worth checking out if you're serious about leveling up your Rust game.
By @andrew_eu - 9 months
It's an excellent book and I agree with many of the comments -- while it's written for Rust, the vast majority of it is applicable to many languages. It's amazing to see the book published on their website for free (though I am still happy in having bought the book).

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?

By @raphar - 9 months
Can you recommend any other book about the same subject but different programming language? Thanks!!!
By @volonoctu - 9 months
Really good book and fairly comprehensive like a course. Probably has the best explanation of memory ordering. The 'build your own' teaching method is useful for understanding how the different data structures work.
By @mbsai29 - 9 months
If you are working with async rust, this book is a must-read. Clearly explains most of the primitives that are used in rust like Arc, Mutex, etc. The examples in the github repo are quite helpful and fairly intuitive if you follow along.
By @skoocda - 9 months
I've done a precursory skim of this and plan to start reading it in earnest next week. Looks comprehensive and accessible. Very excited.
By @bk496 - 9 months
Why can't they distribute the edition as a PDF?
By @pythops - 9 months
Amazing book !
By @quohort - 9 months
Why do programming books always have some random unrelated illustration on the front?

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.