MSDN indicates that mutating access to the .NET type is Dictionary<K,V>not thread safe. Is there a standard version with streaming security?
Dictionary<K,V>
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.
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)
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; } }
Hashtable , , ( , , ).
Hashtable
, Hashtable , .. , - , . , .
, Hashtable , .
. , - , . , .
Dictionary<K, V>, - null, throw KeyNotFoundException, , ( ). , , , , null.
Dictionary<K, V>
null
KeyNotFoundException
.NET 4.0 ConcurrentDictionary<K,T>, , , , . .NET 4.5
ConcurrentDictionary<K,T>
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.
ReaderWriterSlim
Monitor
Source: https://habr.com/ru/post/1707981/More articles:Draw 2D with SlimDX - c #The difference between h1 em and h1> em - cssHow to list all locations in MapPoint 2009 in PlaceCategory using .NET (C # or VB.NET)? - .netBatch file package for output to Python script - python.Net of several event handlers for an event that returns a value - event-handlingSearch for approximate local highs with noisy data in Matlab - matlabUse C # regex to convert shell to string - stringIs there a cross browser way to expand text when css is clicked? - javascriptUIView animations that interact poorly - iphoneWhat is a good fax server? - windowsAll Articles