The documentation of ConcurrentDictionary doesn\'t explicit state, so I guess we cannot expect that delegates valueFactory and u
Not only are these delegates not synchronized, but they are not even guaranteed to happen only once. They can, in fact, be executed multiple times per call to AddOrUpdate.
For example, the algorithm for AddOrUpdate looks something like this.
TValue value;
do
{
if (!TryGetValue(...))
{
value = addValueFactory(key);
if (!TryAddInternal(...))
{
continue;
}
return value;
}
value = updateValueFactory(key);
}
while (!TryUpdate(...))
return value;
Note two things here.
So you need to make sure you do two things.