开发者

Making dictionary access thread-safe? [duplicate]

开发者 https://www.devze.com 2023-01-01 08:26 出处:网络
This question already has answers here: 开发者_JAVA技巧 What's the best way of implementing a thread-safe Dictionary?
This question already has answers here: 开发者_JAVA技巧 What's the best way of implementing a thread-safe Dictionary? (8 answers) Closed 5 years ago.

whats is the easiest way to make C# dictionary access thread safe? Preferably just using lock(object) but any other ideas welcome!


In .NET 4 you have the ConcurrentDictionary class.

If you need to use an older version of .NET, and want to write it yourself:

  • wrap a Dictionary as a private field in your class
  • use a separate private object lockObject
  • take a lock on that lockObject around every access to the dictionary
0

精彩评论

暂无评论...
验证码 换一张
取 消