在本讲里我们会讲清楚为什么像
List<T> 和
Dictionary<TKey, TValue> 这样的普通集合在多线程下会出问题(race conditions),为什么简单的
lock 往往不够,和
System.Collections.Concurrent 里的线程安全集合如何解决这些问题。我们会看几个主要的结构:
ConcurrentQueue<T>,
ConcurrentStack<T>,
ConcurrentBag<T>,
ConcurrentDictionary<TKey, TValue>,它们的使用场景(producer–consumer、缓存、对象池)以及如何选择。