-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Labels
Description
See the implementation:
Lines 1033 to 1037 in e2881ae
| TValue resultingValue; | |
| if (!TryGetValueInternal(key, hashcode, out resultingValue)) | |
| { | |
| TryAddInternal(key, hashcode, valueFactory(key), false, true, out resultingValue); | |
| } |
Two problems here:
- Multiple threads may check
TryGetValueInternalthen gets false and callsTryAddInternalwhich causes the factory method execuites multiple times. If the factory is doing a heavy work, this is a significiant problem. - If the factory method is using another shared object, then factory method will be concurrently executed and the shared object will be concurrently accessed, which can be a bug source (we had this problem)
I think GetOrAdd should be completely thread safe since we trust in the ConcurrentDictionary, use it without checking its internal implementation.
Reactions are currently unavailable