[英]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.所以,我正在编写一些代码,显然 RA 并没有警告我关于所有权如何与 lambda 一起使用的一些错误内容。
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
.但我不明白的是,如果
vec_rc
不是mut
,这是如何工作的。
From my prior knowledge, mutable in Rust cascades;根据我的先验知识,在 Rust 级联中是可变的; 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;
根据我的先验知识,在 Rust 级联中是可变的; 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
).基本的内部可变性原语是
UnsafeCell
,但在它之上构建了多个抽象——其中之一是RwLock
(你可以看到它秘密地包含一个UnsafeCell
)。
As a side note, Rc<RwLock<T>>
is almost always wrong: Rc
is non thread safe, which defeats the whole purpose of RwLock
.作为旁注,
Rc<RwLock<T>>
几乎总是错误的: Rc
是非线程安全的,这违背了RwLock
的全部目的。 If you just need shared mutable stated over one thread, use Rc<RefCell<T>>
.如果您只需要在一个线程上声明共享可变,请使用
Rc<RefCell<T>>
。 It is much more performant and can't block (so no deadlock debugging, just a simple panic if something went wrong).它的性能要高得多并且不能阻塞(所以没有死锁调试,如果出现问题只是一个简单的恐慌)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.