Partially thread-safe dictionary

前端 未结 4 1412
野性不改
野性不改 2021-01-20 15:16

I have a class that maintains a private Dictionary instance that caches some data.

The class writes to the dictionary from multiple threads using a

4条回答
  •  伪装坚强ぢ
    2021-01-20 15:29

    If you want a snapshot of the current state of the dictionary, there's really nothing else you can do with this collection type. This is the same technique used by the ConcurrentDictionary.Values property.

    If you don't mind throwing an InvalidOperationException if the collection is modified while you are enumerating it, you could just return cache.Values since it's readonly (and thus can't corrupt the dictionary data).

提交回复
热议问题