且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字典中读取可能存在或不存在的值的线程安全方法

更新时间:2023-11-26 18:21:22

即使在并发编写者在场的情况下,即使阅读也需要锁定.所以,是的,这和变异字典一样好.

Locking is required even for reading in the presence of concurrent writers. So yes, this is as good as it gets if you mutate the dictionary.

当然,每次写东西时,您总是可以为整个词典创建一个副本.这样,读者可能会看到过时的版本,但他们可以放心地阅读.

You can of course always create a copy of the entire dictionary each time something is written. That way readers might see an out-of-date version but they can safely read.