简体   繁体   中英

How does `write` mode on a `RwLock` work if it isn't mutable?

So, I was writing some code and apparently RA didn't warn me about some erroneous stuff I had written in regards to how ownership works with lambdas.
So, a friend helped me rewrite some of my code, and this is just a play example, but their new code boils down to this:

let vec = Rc::new(RwLock::new( Vec::new() ));
let vec_rc = vec.clone();
let my_lambda = || -> () {
  vec_rc.write().unwrap().push(/* ... */);
}

But what I don't understand is how this works if vec_rc isn't mut .
From my prior knowledge, mutable in Rust cascades; in other words, if the "master-containing" object is immutable the rest will have to be too.

Could I please get some clarity as to what goes on under the hood?
Or is their code erroneous too?

From my prior knowledge, mutable in Rust cascades; in other words, if the "master-containing" object is immutable the rest will have to be too.

This is almost always true... Until we considerinterior mutability .

Interior mutability is exactly about that: changing a value through a shared reference. Moreover, while there are other shared references to it. The basic interior mutability primitive is UnsafeCell , but there are multiple abstractions built on top of it - one of them is RwLock (you can seeit secretly contains an UnsafeCell ).

As a side note, Rc<RwLock<T>> is almost always wrong: Rc is non thread safe, which defeats the whole purpose of RwLock . If you just need shared mutable stated over one thread, use Rc<RefCell<T>> . It is much more performant and can't block (so no deadlock debugging, just a simple panic if something went wrong).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM