If I have a ConcurrentDictionary
object ConcurrentDictionary<int, Dictionary<string, string>>() dict;
, is the nested Dictionary
locked when operations are being performed on the outer ConcurrentDictionary
?
Scenario: an outer ConcurrentDictionary
outerDict
is performing an
outerDict.Add(42, new Dictionary<string, string>())
on one thread, and, on another thread (simultaneously), an inner Dictionary
is performing an
outerDict[30].Add("hello", "world")
.
Is concurrency applied to the modification of both the outer ConcurrentDictionary
and the nested Dictionary
in the above scenario, or are both operations performed at the same time?
CodePudding user response:
Of course not, they're different dictionaries with different access rules.
Your example however is fine, because you're accessing different dictionaries from different threads. If you were to do this instead:
outerDict[30]["a"] = "b"; // in thread 1
outerDict[30]["g"] = "h"; // in thread 2
You'd quickly run into issues.
CodePudding user response:
There are 4 operations here:
- creating a new object. Doing that has no concurrency impact, since nobody else has a reference to the object at that time.
- adding the object to the concurrent dictionary. No problem, because it's a concurrent dictionary.
- accessing the 30th entry in the concurrent dictionary. No problem, because it's a concurrent dictionary
- adding a new value to that 30th dictionary: problematic, if another thread has a reference to the 30th dictionary as well.