placeholder

Why Rust mutexes look like they do - Cliffle

One of the common complaints I hear from systems programmers who try Rust is about mutexes, and specifically about the Rust Mutex API. The complaints usually go something like this:

Click to view the original at cliffle.com

Hasnain says:

Great read combining a language comparison and a discussion of API design tradeoffs.

"Now that we understand why the Rust API is structured as it is, it’s worth asking – why is the C mutex API structured in a way that is hard to use and trivial to misuse, requiring elaborate comments or even static analysis to get right? This, despite the standard API being designed circa 2010, well into the era of commodity multicore processors.

The question is simultaneously fair and unfair. There are important language features missing from C (and C++) that make it impossible to implement a Rust-style mutex API with the same guarantees – lack of explicit lifetimes, absence of an equivalent to Sync, lack of well-defined “move semantics” for ensuring that values end their lives at controlled moments (like with MutexGuard). So, it’s unreasonable to expect the C standard to define a safe mutex API.

But it is not unreasonable to use better tools."

Posted on 2022-04-02T20:06:48+0000