dotnet / dotnet-api-docs

.NET API reference documentation (.NET 5+, .NET Core, .NET Framework)
https://docs.microsoft.com/dotnet/api/
Other
735 stars 1.57k forks source link

sample for ConcurrentDictionaryis not thread safety #1121

Open superyyrrzz opened 7 years ago

superyyrrzz commented 7 years ago

This article has a link to this code sample. In this sample, existingVal is changed on the existing object and returned. In this manner, existingVal.lastQueryDate and existingVal.RecentHighTemperatures may be inconsistent if another thread is modifying it at the same time. An solution is to return an new object, then AddOrUpdate can handle the change correctly.

Source code: https://referencesource.microsoft.com/#mscorlib/system/Collections/Concurrent/ConcurrentDictionary.cs,1151

carlossanlop commented 4 years ago

Tagging area owners @eiriktsarpalis @layomia for triaging.