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?
Many common operations on a dict are atomic, meaning that they are thread-safe. Atomic means that the operation either occurs or does not occur with no in between inconsistent state. Operations such as adding, removing, and reading a value on a dict are atomic.
As you know, Microsoft in C# already provided a generic collection that is called Dictionary. So why do we need ConcurrentDictionary in C#? The answer is that ConcurrentDictionary provides a thread-safe functionality.
ConcurrentDictionary is thread-safe collection class to store key/value pairs. It internally uses locking to provide you a thread-safe class. It provides different methods as compared to Dictionary class. We can use TryAdd, TryUpdate, TryRemove, and TryGetValue to do CRUD operations on ConcurrentDictionary.
Is it OK to concurrently read a Dictionary?
Reading the fine manual yields:
Dictionary<TKey, TValue>
ClassThread 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.)
Yes, reading a Dictionary
concurrently is a perfectly valid operation. According to the thread safety section of the documentation,
A
Dictionary<TKey,TValue>
can support multiple readers concurrently, as long as the collection is not modified.
This is not limited to dictionaries: all data structures are thread-safe in read-only mode. This is the reason why immutable data structures are used for thread safety.
how does the ConcurrentDictionary serve the R/W by multiple threads?
It uses fine-grained locking around write operations to ensure thread safety. Read operations on the dictionary are performed in a lock-free manner.
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