Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to concurrently read a Dictionary? [duplicate]

Tags:

c#

.net

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?

like image 213
smwikipedia Avatar asked Jul 05 '14 13:07

smwikipedia


People also ask

Are Python dict threads safe?

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.

Is C# Dictionary thread-safe?

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.

How does a concurrent Dictionary work?

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.


2 Answers

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.)

like image 159
ta.speot.is Avatar answered Sep 26 '22 02:09

ta.speot.is


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.

like image 33
Sergey Kalinichenko Avatar answered Sep 25 '22 02:09

Sergey Kalinichenko