Jon*_*Jon 77 .net c# concurrency multithreading .net-4.0
我是否正确地认为这是正确使用并发字典
private ConcurrentDictionary<int,long> myDic = new ConcurrentDictionary<int,long>();
//Main thread at program startup
for(int i = 0; i < 4; i++)
{
myDic.Add(i, 0);
}
//Seperate threads use this to update a value
myDic[InputID] = newLongValue;
Run Code Online (Sandbox Code Playgroud)
我没有锁等,只是更新字典中的值,即使多个线程可能尝试做同样的事情.
Ode*_*ded 68
这取决于线程安全的含义.
从MSDN - 如何:从ConcurrentDictionary添加和删除项目:
ConcurrentDictionary<TKey, TValue>适用于多线程场景.您不必在代码中使用锁来添加或删除集合中的项.但是,一个线程始终可以检索一个值,而另一个线程可以通过为相同的键提供一个新值来立即更新该集合.
因此,可以获得字典中项目值的不一致视图.
| 归档时间: |
|
| 查看次数: |
71739 次 |
| 最近记录: |