37

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?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482

2 Answers2

42

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

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • 7
    That's just pasting from the documentation. This should just be a link in a comment, not an answer. – Guffa Jul 05 '14 at 13:53
  • 21
    There's a lesson in my answer: "[read] the fine manual". So important it's in there twice! – ta.speot.is Jul 05 '14 at 13:54
  • 3
    The documentation, the way I understand it, only states *enumeration* shouldn't be performed concurrently with writing to the dictionary. It doesn't state if concurrent random reads and writes are safe. – nietaki Jan 08 '16 at 16:31
  • 2
    @nietaki Are you talking about `Dictionary` or `ConcurrentDictionary`? `Dictionary` says they're not safe: *To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization* – ta.speot.is Jan 09 '16 at 00:21
  • 1
    It might be worth it to make this bit of the documentation bold in the answer as well, so that people don't get fixated on the enumeration part before. – nietaki Jan 12 '16 at 15:44
32

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523