Ditch your (mut)ex, you deserve better

https://news.ycombinator.com/rss Hits: 10
Summary

Having access to multiple parallel CPU cores isn't a new thing by any means, people have been programming in parallel for half a century now, but recent years we've found ourselves at an inflection point. Moore's law is dying, beefy single cores are no longer keeping up. Modern computers come with multiple CPU cores, so exploiting parallel compute is more important than ever. Given how long it's been an area of research we can naturally expect that effective tools have taken root and that synchronizing threads is trivial now right...? Unfortunately this has not been my experience, and I'm willing to bet it hasn't been yours either. Managing shared state across threads is hard, and the most commonly used tools: mutexes and semaphores, simply haven't evolved much since their inception. The words that follow will dig into the problems inherent to mutexes and synchronizing shared mutable state. Afterwards we'll look into other avenues which should prove more helpful. The Problem with Shared State Let's begin by crafting a simple software system which needs synchronization in the first place. I'll present a commonly used example: the task of managing bank account balances correctly in spite of parallel transfer requests. Of course real banks don't store all their account balances in RAM, so I'll hope that the reader can apply the concepts from this pedagogical example to a their own domain as necessary, it serves as a stand-in for any sufficiently complex system which requires ad-hoc synchronization of arbitrary data between multiple threads. Here's some golang'ish pseudo-code (please don't try to actually compile it) for a simple bank account and the operations upon it. I'm focused on the synchronization problems here, so forgive me for skipping the double-entry accounting, input validation, and other real-world complexities. struct Account { balance int, } // Deposit money into an account func (a *Account) deposit(amount int) { a.balance += amount } // Withdraw money f...

First seen: 2025-11-18 07:49

Last seen: 2025-11-18 18:51