Recent Posts
Is Parallel Programming Hard [07]: Locking
Claude 根据原.tex格式生成 markdown, 前面还行, 后边稀烂.
Locking Locking is the worst general-purpose synchronization mechanism except for all those other mechanisms that have been tried from time to time.
— With apologies to the memory of Winston Churchill and to whoever he was quoting
In recent concurrency research, locking often plays the role of villain. Locking stands accused of inciting deadlocks, convoying, starvation, unfairness, data races, and all manner of other concurrency sins.
read more
Rust Variance
RustVariance 最近学习 Bevy 源码,参考大佬的 Bevy之书 看到 UnsafeWorldCell 时,
// 源码: crates/bevy_ecs/src/world/unsafe_world_cell.rs:84 pub struct UnsafeWorldCell<'w> { ptr: *mut World, #[cfg(debug_assertions)] allows_mutable_access: bool, _marker: PhantomData<(&'w World, &'w UnsafeCell<World>)>, } 其中 _marker 签名有点让人困惑,涉及 Rust variance 的知识点,之前这里一直没有弄明白,知道有这个东西,但是不知道什么时候用。 主要就是参考官方文档 [1],[2], 然后就是一顿问 Claude。 大致就是 &'w World 对 'w 是协变(covariant), &'w UnsafeCell<World> 没啥用,也是协变作用. 如果要是不变(invariant)的话, 就要写成 UnsafeCell<&'w World>, 这里还有让UnsafeWorldCell为!Sync? 参考官方文档 [2] 的表: PhantomData> invariant - Send + !Sync allowed Cell<&'a ()> 才能对 'a 不变.
这个结论也可以参考 bevy 的当时的 commit, 其中 comment 提到:
read more