TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust Atomics and Locks (2023)

209 pointsby 0xedb9 months ago

13 comments

tialaramex9 months ago
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&#x27;s Mutex and RwLock were implemented on Windows (by the author in fact IIRC).<p>Since then, two important things happened.<p>1. On Windows 8 and beyond Rust moved to WaitOnAddress with an API similar to the futex on several other systems.<p>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&#x27;s API said about them. This bug is fixed... in Microsoft&#x27;s own version control, not in released Windows versions.<p>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 <i>also</i> get a read lock, which would ordinarily be possible - too bad, you&#x27;ve secretly been given the exclusive write lock so read locks are unavailable until you release it.<p>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&#x27;s a different function call) whether you asked for a <i>read</i> or a <i>write</i> lock. Since they didn&#x27;t have anywhere to store this single bit (read or write) they just assumed they don&#x27;t know in this edge case where the lock happens to be available immediately, and since they &quot;don&#x27;t know&quot; they always give you a write lock anyway. Oops.<p>[Edited to make minor clarifications]
评论 #41241324 未加载
评论 #41241446 未加载
hobofan9 months ago
Even if you are not into Rust, I&#x27;d recommend this book if you want to get into low-level and&#x2F;or embedded programming. It&#x27;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).
评论 #41243049 未加载
评论 #41243949 未加载
nutate9 months ago
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&#x27;s extremely well written, in the weeds, but not lost in them. If you&#x27;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.
ridiculous_fish9 months ago
Any thoughts on the best way to express global locks in Rust?<p>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.<p>But this is an awkward fit for Rust, whose locks want to own the data they protect. What&#x27;s the best way to express a global lock, enforcing that certain data may only be accessed while the global lock is held?
评论 #41241263 未加载
评论 #41241847 未加载
评论 #41241099 未加载
评论 #41241163 未加载
评论 #41242563 未加载
评论 #41241064 未加载
ra0x39 months ago
If you&#x27;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&#x27;re serious about leveling up your Rust game.
andrew_eu9 months ago
It&#x27;s an excellent book and I agree with many of the comments -- while it&#x27;s written for Rust, the vast majority of it is applicable to many languages. It&#x27;s amazing to see the book published on their website for free (though I am still happy in having bought the book).<p>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&#x2F;medium-sized project (weekend sized) which would actually use the patterns discussed in the book?
raphar9 months ago
Can you recommend any other book about the same subject but different programming language? Thanks!!!
评论 #41241187 未加载
评论 #41240936 未加载
评论 #41240821 未加载
评论 #41240938 未加载
评论 #41247345 未加载
评论 #41240792 未加载
评论 #41241851 未加载
评论 #41240829 未加载
评论 #41243970 未加载
评论 #41244699 未加载
volonoctu9 months ago
Really good book and fairly comprehensive like a course. Probably has the best explanation of memory ordering. The &#x27;build your own&#x27; teaching method is useful for understanding how the different data structures work.
mbsai299 months ago
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.
评论 #41240944 未加载
skoocda9 months ago
I&#x27;ve done a precursory skim of this and plan to start reading it in earnest next week. Looks comprehensive and accessible. Very excited.
bk4969 months ago
Why can&#x27;t they distribute the edition as a PDF?
pythops9 months ago
Amazing book !
quohort9 months ago
Why do programming books always have some random unrelated illustration on the front?<p>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&#x27;t really such an equivalent unless it&#x27;s a computer graphics book.<p>I guess it&#x27;s also like how every project has to have its own &quot;cutesey&quot; mascot.
评论 #41243997 未加载
评论 #41241951 未加载