【发布时间】:2019-08-05 18:36:14
【问题描述】:
我正在玩这个玩具结构:
pub struct Community {
pub community_contents: RwLock<CommunityContents>,
}
pub struct CommunityContents {
pub friends: RefCell<HashMap<FriendID, FriendData>>,
pub index: RefCell<HashMap<u64, BTreeMap<FriendID, FriendData>>>,
pub authenticated: bool,
pub age: u64,
pub height: u64,
}
我的理论是在RwLock 的Community 周围有一个Arc,然后我可以有多个写入者到RwLock,这些写入者可以独立/同时修改friends 和index 字段CommunityContents 来自 Rc<RefCell。
这甚至可能吗? RwLock 会不会一次只允许一个以上的作者,在这种情况下,我应该删除 RefCells 并简化整个结构?
【问题讨论】:
-
我建议您阅读我的那个问题的答案以了解全貌:stackoverflow.com/a/45674912/4498831
-
我认为这是一个副本(上面链接的 Q),但经过反思我不太确定。我认为这个问题实际上太宽泛了,无法回答——你能详细说明“通过
Rc<RefCell>同时修改[...]”是什么意思吗?还是@FrenchBoiethios 链接的问题也回答了您的问题? -
@trentcl 我的意思是让线程同时修改朋友和索引字段。一组线程修改好友同时索引好友被另一个线程修改。
-
@Gmanman 好吧,你只需要细化你的锁。不要将整个社区放在一个 RwLock 中,而是将好友和索引放在单独的 RwLock 中
标签: rust locking readerwriterlock