Inside Rust's std and parking_lot mutexes – who wins?

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

A while ago, our team was working on a Rust project where std::sync::Mutex was everywhere. A team member suggested switching to parking_lot::Mutex instead. They heard that it has better performance, smaller memory footprint, and more predictable behavior under contention.I had no idea how to evaluate this claim. A quick search online returned results favoring parking_lot. This felt wrong to me. Why? It contradicted my belief that std should be the gold standard. The standard library team knows what they’re doing, right? And if parking_lot’s mutex really was the performance winner, there had to be trade-offs between the two implementations that people weren’t talking about.That mystery haunted me. I couldn’t just take it on faith. So I jumped down the rabbit hole: read both implementations, wrote the benchmarks, and here we are. In this post, I will:Explain how std implements the mutex (v1.90.0)Explain how parking_lot implements their mutex (v0.12.5)Show you the benchmark with key findingsGive you a decision guide for when to use eachBut first, let’s ground our foundation on mutexes (skim it if you’re already familiar).A classic example of the kind of problem that mutex solves is withdrawing and receiving money at the same time. Imagine you have $100 in your account. Thread A tries to withdraw $80, and Thread B tries to deposit $50. Without proper synchronization, both threads might read the balance as $100 simultaneously, then write back their results independently:Mutex solves this nicely by having a thread wait until the other finishes its update:The operations that read and write the balance are what we need to protect - these are called critical sections. Any code that accesses shared data needs to be inside a critical section, guarded by a mutex.Simple enough, right? Now let’s see how to use a mutex. (Again, skim this if it’s too basic for you)In languages other than Rust, you typically declare a mutex separately from your data, then manually lock it before ent...

First seen: 2025-11-24 16:22

Last seen: 2025-11-25 14:25