Seems like missed opportunity (Condvar)

Condvar example:

let pair = Arc::new((Mutex::new(false), Condvar::new()));

I wonder why it wasn't done like this (which is how it is done with most? other types):

let cond = Arc::new(Mutex::new(Condvar::new(false)));

I believe this not only reads better as well as it is always the same for any type:


Instead we have for CondVar:


What I'm getting at that, is that instead of having CondVar somewhat next to Mutex, we could have it protected by Mutex. This would also mean less code, we would be sure that we only access the condvar is safe way etc.
Also, we could have easier way to actually use it:

let cond_var: Arc::new(Mutex::new(Condvar::new(false))));

Any thoughts?

It would prevent notifying the condvar without locking the mutex, which might be useful sometimes. Something that could have been done that would also allow this is to have a Condvar<bool> which internally contains a mutex and the state (I haven't thought this through though). But of course std is stabilized now, so it's not going to change :frowning:

1 Like

Fair enough. Thanks for sharing.

It would also prevent having multiple condition variables for a single mutex.

1 Like

Couldn't it be done in:


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.