1

I know ReadOnlyDictionary is "thread-safe" when accessing from multiple threads as long as the collection isn't changing.
But what if the collection isn't changing (no keys are ever added/removed) and values are thread-safe by themselves, meaning, the reference will not change, but the value's internal data might (the Value object itself is thread safe) For example

ReadOnlyDictionary<Key, ThreadSafe Value> dictionary = new...  
dictionary[key].inc()

Where inc() is a thread safe method of ThreadSafeValue.

Since the collection itself isn't changing, and the references aren't changing, I'd think this is ok, but since ReadOnlyDictionary doesn't expose Add/Remove/Update and it's not thread safe, I wonder if my assumption is correct

SagiLow
  • 5,721
  • 9
  • 60
  • 115
  • `ReadOnlyDictionary` is only as thread safe as the dictionary it wraps (see https://stackoverflow.com/questions/13684143/why-isn-t-readonlydictionary-thread-safe). Therefore, it depends on what dictionary implementation is being used underneath. – Etienne de Martel Dec 18 '21 at 22:42
  • You may treat the question as if it's asked about `Dictionary` but without adding/removing keys.. the question still stands as it's unclear from the documentation what "changing the collection" means – SagiLow Dec 18 '21 at 22:46
  • In that case, this probably answers your question: [Thread safety of a Dictionary](https://stackoverflow.com/questions/2043615/thread-safety-of-a-dictionarytkey-tvalue) (but if it doesn't, feel free to explain why). – Etienne de Martel Dec 18 '21 at 22:49
  • 1
    This question and the replies talks about a dictionary that both the keys and the values are never changed. I'm asking about a dictionary in which the keys aren't changed, but values data (not reference) may change – SagiLow Dec 18 '21 at 22:51
  • I would say that the answer, as per previous comments, is "the ReadOnlyDictionary itself has no effect on the safety of the situation". So, not sure what exactly you are asking. – ToolmakerSteve Dec 18 '21 at 22:57

2 Answers2

3

Your issue seems to stem from a confusion of what a "value" is in the context of a dictionary.

From the perspective of a dictionary, assuming ThreadSafeValue is a reference type, then the value is a reference to the object. If you never modify the dictionary, then the reference itself can never change. In other words, neither the key nor the value changes.

If ThreadSafeValue itself is thread safe, then the whole use case appears safe.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • No, that's not true. Thread safety isn't gained from the sum of the parts. It can only be measured on the whole. It's as faulty as saying that if bricks are solid then if you make a house from bricks then the house is solid too. That's just not the case. Even with thread-safe values in a thread-safe collection the code you write may make it not thread-safe. – Enigmativity Dec 19 '21 at 07:03
  • This isn't remotely what I said. I just talked about this specific use case. – Etienne de Martel Dec 19 '21 at 19:09
  • Yes, so did I. My point is that the code that the OP showed isn't enough to establish thread-safety for how the code is used. It's a moot point unless the specific use case is actually presented. – Enigmativity Dec 19 '21 at 21:05
  • Assuming that any method of `ThreadSafeValue` is thread safe, and that the dictionary indeed never changes while it is being used by multiple threads, what possible thread safety issue could arise here? – Etienne de Martel Dec 19 '21 at 22:36
-1

Yes, your assumption is correct. Since the values stored in the dictionary are autonomously thread-safe, and the dictionary itself is practically immutable, the whole data structure is thread safe.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104