36
you are viewing a single comment's thread
view the rest of the comments
[-] BB_C@programming.dev 0 points 2 days ago

The real reason to not use poisoning mutexes is convenience and developer experience. The standard library should take such considerations heavily into account. A rare use case such as poisoning being required should not be the default for mutexes in the standard library.

What does that even mean?

The interface concerns are as trivial as it gets:

#![feature(nonpoison_mutex)]
#![feature(sync_nonpoison)]

use std::sync::Mutex as PMutex;
use std::sync::nonpoison::Mutex as NoPMutex;

trait LockExt<'a> {
    type Guard;
    fn l(&'a self) -> Self::Guard;
}

impl<'a, T: 'a> LockExt<'a> for PMutex<T> {
    type Guard = std::sync::MutexGuard<'a, T>;
    fn l(&'a self) -> Self::Guard {
       self.lock().expect("non-poisoned") 
    }
}

impl<'a, T: 'a> LockExt<'a> for NoPMutex<T> {
    type Guard = std::sync::nonpoison::MutexGuard<'a, T>;
    fn l(&'a self) -> Self::Guard {
       self.lock() 
    }
}

fn main() {
    let m_p = PMutex::new(false);
    let m_no_p = NoPMutex::new(false);
    *m_p.l() = true;
    *m_no_p.l() = true;
    dbg!(*m_p.l());
    dbg!(*m_no_p.l());
}

This is basically is the one thing that shouldn't consume any mind tokens when it comes to synchronization primitive choice.

[-] SorteKanin@feddit.dk 1 points 2 days ago

I'm not really sure what you're trying to say here. Are you saying that this is not a problem and everyone should just add this LockExt trait to all their code bases that uses Mutexes? Or..?

[-] BB_C@programming.dev 2 points 2 days ago

Every non-trivial code-base already has a utils module (sometimes a whole crate even) containing, in part, extension traits (and sometimes macros) to make some usages of some types nicer, in a way that is specific to the codebase (even in this case, a codebase might want a debug or trace log message for example, alongside the panic).

It's a part of the language for a reason. It provides full idiomacity. And this is not limited to (poisoned or otherwise) synchronization primitives.

Talking about a method returning a Result like it's a big deal must be some sort of splitting-hairs maxxing.

And btw, as it just happens, such traits actually already exist, in a crate that is well-known to the ecosystem.

[-] SorteKanin@feddit.dk 1 points 2 days ago

in a way that is specific to the codebase

Well I would say this is not at all specific to the codebase - as I said above, it applies to basically all code bases that use mutexes, since poisoning is so rarely relevant.

These two statements are kind of strange to me:

Talking about a method returning a Result like it’s a big deal must be some sort of splitting-hairs maxxing.

and

And btw, as it just happens, such traits actually already exist, in a crate that is well-known to the ecosystem.

If this is such a small deal, then using an external dependency is a terrible solution. That's like the whole left-pad situation, a dependency to solve an extremely small problem, but something that affects many, many code bases. That's exactly the use case that the standard library should cover, exactly so that you don't need to use an external dependency for such a trivial thing!

And yea you could say "duh just write the extension trait in your utils module" but this is just not acknowledging the problem at all and just sidestepping it. That's not helping anyone.

[-] BB_C@programming.dev 1 points 2 days ago

Well I would say this is not at all specific to the codebase

I was talking about what a codebase would want a non-Result-returning lock() to look like (the panic message, or forcing abort, extra logging, signal handling...etc).

since poisoning is so rarely relevant.

Poisoning (as a negative) is only relevant if we are pretending it matters for performance. And you already conceded that this is not a part of the conversation as far as you're concerned.

The "rarely relevant" (as in playing any operative role at all) is also pure conjecture, or if we want to be nicer, it's totally contextual and use-case dependant.

then using an external dependency is a terrible solution.

lock_api provides a generic interface so you can e.g. seamlessly swipe out primitives from parking_lot with primitives from spin, you know, the whole point behind generic programming!

This is in no way comparable with leftpad. And I wasn't actually pointing to it as a solution (to the largely non-existing problem). I just wanted to point out that extension traits for synchronisation primitives already exist with wide usage in the ecosystem, since you seemed to not be aware.

If you have an aversion to extension traits. Here is a trivial unit struct wrapper solution. You don't even need to change the Mutex type name or the lock() method name in your code.

use std::ops::{Deref, DerefMut};
use std::sync::Mutex as PMutex;

#[repr(transparent)]
struct Mutex<T>(PMutex<T>);

impl<T> Deref for Mutex<T> {
    type Target = PMutex<T>;
    fn deref(&self) -> &Self::Target { &self.0 }
}

impl<T> DerefMut for Mutex<T> {
    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}

impl<T> Mutex<T> {
    fn new(v: T) -> Self {
        Self(PMutex::new(v))
    }

    fn lock<'a>(&'a self) ->  std::sync::MutexGuard<'a, T> {
        self.0.lock().expect("poisoned")
    }
}

fn main() {
    let m = Mutex::new(false);
    // oh look, it's a non-Result returning lock()
    *m.lock() = true;
    dbg!(*m.lock());
    // inner Mutex method API is still available via Deref/DerefMut
    *m.try_lock().unwrap() = false;
    dbg!(*m.lock());
}

And yea you could say “duh just write the extension trait in your utils module” but this is just not acknowledging the problem at all and just sidestepping it. That’s not helping anyone.

What is the notable relevant problem. Can you state it plainly?

[-] SorteKanin@feddit.dk 1 points 2 days ago

I already told you, convenience, developer experience. Ergonomics basically. None of this is fixed by some code snippet, whether it's an extension trait or wrapper type. That just makes things worse. This has nothing to do with performance, as I already said.

[-] BB_C@programming.dev 1 points 2 days ago

convenience, developer experience. Ergonomics basically.

Can you point the notable relevant inconvenience, bad developer experience, and poor ergonomics developers are suffering from?

Is it all caused by a lock() method returning a Result?
Anything else?

[-] SorteKanin@feddit.dk 2 points 1 day ago

Yes? I think I am done with this conversation. If you don't think this is a problem, we just disagree. Just move on, you don't have to convince everyone else who's been discussing this problem for years that it isn't a problem.

[-] BB_C@programming.dev 0 points 1 day ago

everyone else who’s been discussing this problem for years that it isn’t a problem.

I haven't seen anyone else concede the performance talking point (under different banners), but proceeding to make this out to be a big issue. This includes this thread's OP which literally starts on-topic arguments with:

How poisoning harms every single piece of code

As discussed in too much detail above, (un)locking mutexes is cheap as long as they’re not contended, and when contended, they are at least cheap instruction wise. This is where poisoning mutexes get you.

This is what makes your positioning weird.

My replies here, starting with pointing out the lack of benchmarks of real-world use-cases, come from actually bothering to read OP, IN FULL.

this post was submitted on 18 Nov 2026
36 points (100.0% liked)

Rust

8379 readers
30 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 3 years ago
MODERATORS