dotnet / dotnet/dotnet-api-docs

sample for ConcurrentDictionaryis not thread safety

Open
#1,121 1 comment 0 reactions 0 assignees View on GitHub
area-System.Collections help wanted Pri3 untriaged
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.