logoalt Hacker News

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

113 pointsby signa11last Thursday at 5:32 AM43 commentsview on HN

Comments

kccqzytoday at 4:52 PM

There was a giant super-long GitHub issue about improving Rust std mutexes a few years back. Prior to that issue Rust was using something much worse, pthread_mutex_t. It explained the main reason why the standard library could not just adopt parking_lot mutexes:

From https://github.com/rust-lang/rust/issues/93740

> One of the problems with replacing std's lock implementations by parking_lot is that parking_lot allocates memory for its global hash table. A Rust program can define its own custom allocator, and such a custom allocator will likely use the standard library's locks, creating a cyclic dependency problem where you can't allocate memory without locking, but you can't lock without first allocating the hash table.

> After some discussion, the consensus was to providing the locks as 'thinnest possible wrapper' around the native lock APIs as long as they are still small, efficient, and const constructible. This means SRW locks on Windows, and futex-based locks on Linux, some BSDs, and Wasm.

> This means that on platforms like Linux and Windows, the operating system will be responsible for managing the waiting queues of the locks, such that any kernel improvements and features like debugging facilities in this area are directly available for Rust programs.

show 3 replies
pizlonatortoday at 4:17 PM

Author of the original WTF::ParkingLot here (what rust’s parking_lot is based on).

I’m surprised that this only compared to std on one platform (Linux).

The main benefit of parking lot is that it makes locks very small, which then encourages the use of fine grained locking. For example, in JavaScriptCore (ParkingLot’s first customer), we stuff a 2-bit lock into every object header - so if there is ever a need to do some locking for internal VM reasons on any object we can do that without increasing the size of the object

show 2 replies
ballpugtoday at 9:54 PM

For Cargo.toml, an error: invalid basic string, expected `"` for 10:11 std/parking_lot_mutexes.

Sourcing VS, documentation indicates Python, C/C++, GitHubCopilot, and an Extension Pack for Java in top extensions.

[1]: https://code.visualstudio.com/docs

adzmtoday at 4:59 PM

The original webkit blog post about parking lot mutex implementation is a great read https://webkit.org/blog/6161/locking-in-webkit/

kouteiheikatoday at 5:51 PM

> Poisoning: Panic Safety in Mutexes

This is one of the biggest design flaws in Rust's std, in my opinion.

Poisoning mutexes can have its use, but it's very rare in practice. Usually it's a huge misfeature that only introduces problems. More often than not panicking in a critical section is fine[1], but on the other hand poisoning a Mutex is a very convenient avenue for a denial-of-service attack, since a poisoned Mutex will just completely brick a given critical section.

I'm not saying such a project doesn't exist, but I don't think I've ever seen a project which does anything sensible with Mutex's `Poisoned` error besides ignoring it. It's always either an `unwrap` (and we know how well that can go [2]), or do the sensible thing and do this ridiculous song-and-dance:

    let guard = match mutex {
        Ok(guard) => guard,
        Err(poisoned) => poisoned.into_inner()
    };
Suffice to say, it's a pain.

So in a lot of projects when I need a mutex I just add `parking_lot`, because its performance is stellar, and it doesn't have the poisoning insanity to deal with.

[1] -- obviously it depends on a case-by-case basis, but if you're using such a low level primitive you should know what you're doing

[2] -- https://blog.cloudflare.com/18-november-2025-outage/#memory-...

show 4 replies
kccqzytoday at 7:10 PM

I will personally recommend that unless you are writing performance sensitive code*, don’t use mutexes at all because they are too low-level an abstraction. Use MPSC queues for example, or something like RCU. I find these abstractions much more developer friendly.

*: You may be, since you are using Rust.

show 2 replies
Shelby-Thomastoday at 6:22 PM

[dead]

Barry-Perkinstoday at 7:32 PM

[dead]

mgaunardtoday at 8:20 PM

tl;dr: the implementation that is designed for fairness has lower standard deviation under contention, but otherwise performs slightly worse.

Nothing too surprising.