I use a ConcurrentDictioanry<string, HashSet<string>>
to access some data across many threads.
I read in this article (scroll down) that the method AddOrUpdate
is not executed in the lock, so it could endanger thread-safety.
My code is as follows:
//keys and bar are not the concern here
ConcurrentDictioanry<string, HashSet<string>> foo = new ...;
foreach(var key in keys) {
foo.AddOrUpdate(key, new HashSet<string> { bar }, (key, val) => {
val.Add(bar);
return val;
});
}
Should I enclose the AddOrUpdate
call in a lock
statement in order to be sure everything is thread-safe?