Search code examples
c#multithreadingconcurrencythread-safety.net-8.0

Is FrozenDictionary<TKey,TValue> thread safe in .NET 8?


Is it safe to read concurrently from an instance of FrozenDictionary<TKey,TValue> from different threads?

I assume this type being thread safe, since it is immutable, but curiously its thread safety is not explicitly documented (I can't find any mention to it by looking at the official documentation. Am I looking at the wrong place?).

In my experience, thread safety is always explicitly documented in Microsoft documentation and it is never granted as "implicit". For instance, the thread safety of ImmutableDictionary<TKey,TValue> is explicitly documented.

Does anyone has a clue on this?


Solution

  • In my considered opinion, this is a documentation bug and should be addressed as such; the intent is I'm 99.9% positive that this is a truly immutable and read-only entity, and if we ignore reflection abuse (because all bets are off there, on any type): it is thread-safe. The constructor is internal, so we don't need to be concerned about exotic custom implementations - just the in-box ones (empty, small, default, etc), which all appear (on a cursory inspection) to be implemented appropriately (this is not a definitive review, note).

    IMO: log a documentation issue