private object lockObj = new object();
private Dictionary<int, string> dict = new Dictionary<int, string>();
public string GetOrAddFromDict(int key)
{
string value;
// non-locked access:
if (dict.TryGetValue(key, out value))
return value;
lock (this.lockObj)
{
if (dict.TryGetValue(key, out value))
return value;
string newValue = "value of " + key; // place long operation here
dict.Add(key, newValue);
return newValue;
}
}
问题a:它是线程安全的吗?如果是,为什么?
问题 b:这个双 TryGetValue() 模式是如何调用的?
最佳答案
a) 这不是线程安全的,因为底层 Dictionary
本身不是线程安全的。如果另一个线程同时调用 Add,可能会发生未定义的行为。
b) 这实际上是对 double-checked locking 的尝试.
我建议使用 ConcurrentDictionary类,因为它是为这种情况设计的。另一种选择是使用 ReaderWriterLockSlim (或 ReaderWriterLock ),如果您的目标不是 .NET 4.0。
关于c# - 这个非锁定的 TryGetValue() 字典访问线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6738634/