Standard .Net Dictionary.

MSDN indicates that mutating access to the .NET type is Dictionary<K,V>not thread safe. Is there a standard version with streaming security?

Note: “No, no” is the correct answer if it is true. In these cases (and it seems that way) I will leave and do the blocking thing.


Almost duplicate

What is the best way to implement a thread safe dictionary in .NET? (Inaccurate, because I want a standard type, and not what I implement myself)

+3
source share
4 answers

No. Consider this code where each method / property was thread safe

if (!SFdict.Contains(key))
{
   SFdict[key] = value;
}

, , b/c . -

lock(lck)
{
   if (!dict.Contains(key))
   {
      dict[key] = value;
   }
}
+3

Hashtable , , ( , , ).

, Hashtable , .

. , - , . , .

Dictionary<K, V>, - null, throw KeyNotFoundException, , ( ). , , , , null.

+2
+2

In addition to answering a duplicate question, you can take a look at this implementation that usesReaderWriterSlim . ReaderWriterSlimshould understand some of the performance benefits with a simple lock (which effectively leverages Monitor) - definitely take a look.

+1
source

Source: https://habr.com/ru/post/1707981/


All Articles