Is it OK to concurrently read a Dictionary?
.net, c#
Solution
Is it OK to concurrently read a Dictionary?
Reading the fine manual yields:
`Dictionary<TKey, TValue>` Class
Thread Safety
A Dictionary can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
For modifications and write operations to the dictionary, ConcurrentDictionary uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.)
As for:
how does the ConcurrentDictionary serve the R/W by multiple threads?
Reading the fine manual yields:
`ConcurrentDictionary<TKey, TValue> Class`
Remarks
For modifications and write operations to the dictionary, ConcurrentDictionary uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.)
Problem
There's a ConcurrentDictionary type for concurrently read and write operation. Since there's only read operation in my scenario, I am wondering if it is OK to just use the Dictionary? And btw, how does the ConcurrentDictionary serve the R/W by multiple threads? Does it use some kind of lock implicitly to make all R/W operations serialized?