Am I right in thinking this is the correct use of a Concurrent Dictionary
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;
I have no locks etc and am just updating the value in the dictionary even though multiple threads might be trying to do the same.
newLongValue
depend on the previous value ofmyDic[InputID]
? – Damien_The_Unbeliever Nov 22 '11 at 11:55myDic[InputID]
for race condition. You should tryGetOrAdd
– Olivier Albertini Sep 4 '16 at 23:17myDic[InputID]
causes any problem when it is used as an lvalue.GetOrAdd
is not a correct replacement since it adds only if value does not exist. We can instead useAddOrUpdate
to add/update same value in dictionary. – Jatin Sanghvi Jul 9 '17 at 0:10