I’ve got code like this that’s executed from many threads simultaneously (over shared a and b objects of type Dictionary<int, double>):
foreach (var key in a.Keys.Union(b.Keys)) {
dist += Math.Pow(b[key] - a[key], 2);
}
The dictionaries don’t change during the lifetime of the threads. Is this safe? So far, it seems OK, but I wanted to be sure.
From the dictionary documentation:
As long as you’re never writing, it should be safe.