If I have a
Dictionary<int, StreamReader> myDic = new Dictionary<int, StreamReader>
//Populate dictionary
One thread does
myDic[0] = new StreamReader(path);
Another thread does
myDic[1] = new StreamReader(otherpath)
Is this thread safe because the actual item in the dictionary getting modified is different to the one on the other thread or will I get a InvalidOperationException: Collection was modified
Most Dictionary Operations Are AtomicMany common operations on a dict are atomic, meaning that they are thread-safe.
Not only is it not thread safe; it will also throw with NullReferenceException if accessed while another thread is reorganizing the hash buckets. The lock statement is wicked fast, don't avoid it.
Dictionaries in Swift are not thread safe , they lead to wierd crashes which are very hard to debug. This class solves this problem by using a dictionary whose accesses are made thread safe by using a concurrent queue with a barrier.
You will only get InvalidOperationException: Collection was modified
if you enumerate the dictionary while modifying.
However, that is not thread-safe.
If one of those operations causes the dictionary to resize, the other one may get lost.
Instead, use ConcurrentDictionary
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With