dotnet / dotnet/dotnet-api-docs
sample for ConcurrentDictionaryis not thread safety
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
[This](https://github.com/dotnet/docs/blob/master/docs/standard/collections/thread-safe/how-to-add-and-remove-items.md) article has a link to [this code sample](https://github.com/dotnet/docs/blob/master/samples/snippets/csharp/VS_Snippets_Misc/cds/cs/cds_dictionaryhowto.cs#L144). 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
Contributor guide
Assessment
This issue has not been assessed yet.