I know that dictionaries are not thread safe.
But for sake of simplicity I sometimes (when the scenario is not too complex) wrap them around a lock and used it in multi-thread environment even if other concurrent structure may be used (eg : ConcurrentDictionary
).
Here an example of what I am doing :
public class Example
{
private object _sync = new object();
private Dictionary<string,stirng> _dictionary = new Dictionary<string,string> ();
public void Write (string key, stirng value)
{
lock(_sync)
{
if (_dictionary.ContainsKey(key) == false)
_dictionary.Add(key,value);
}
}
public string Read (string key)
{
if (_dictionary.ContainsKey(key))
return _dictionary[key];
return null;
}
}
I am afraid that maybe even reading the dictionary while writing, may produce inconsistent data.
I am not scared about reading old data, and not "seeing" the new one (till the write end) that's ok in my scenario.
Even if the new data is the only inconsistent one (till the write operation end) it's ok.
What I am concerned about is that maybe the dictionary structure is in a usable state while inserting (and so a read in that moment will produce a complete messed up result even for very old key and not only the new one).