【发布时间】:2017-10-30 16:37:44
【问题描述】:
这个问题主要是学术性的 - 我知道这不一定是实用代码。
考虑一下这段代码,其中线程安全是一个问题:
// In the constructor
IDictionary<string, string> myDictionary = new ConcurrentDictionary<string, string>();
...
// Elsewhere in the class
myDictionary.Add("foo", "bar");
我的总体问题:从并发的角度来看这是如何处理的?
这会被认为是线程安全的吗?如果是,为什么?我问是因为(已知的)线程安全方法是AddOrUpdate - 我无法使用IDictionary 访问它。 CLR 是否“足够聪明”知道这里发生了什么?我在这里是否缺少继承属性,.Add 调用是否可以突变为 .AddOrUpdate 调用“幕后”?
【问题讨论】:
标签: c# .net concurrency clr