dotnet / dotnet-api-docs

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

Not very well explained #1098

Open Korporal opened 5 years ago

Korporal commented 5 years ago

The docs about this refer to "thread safe" yet say absolutely nothing about whether multiple threads may enumerate the elements in such a way that no thread ever gets the same element.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

uecasm commented 4 years ago

FWIW, the answer is "no".

All methods internally lock the SyncRoot, including GetEnumerator(), but the actual enumeration is not thread-safe. Calling code will need to externally lock SyncRoot during enumeration.

(Note that it is possible to write a collection class that does have thread-safe enumeration automatically -- and it's very easy to do using C#2 iterators. This particular class does not do so, however, possibly in part because it predates that language feature.)