Thread safety of a Dictionary

前端 未结 2 1728
借酒劲吻你
借酒劲吻你 2020-12-01 14:06

If I initialize a generic dictionary once, and no further adds/updates/removes are allowed, is it safe to have multiple threads reading from it with no locking (assuming tha

相关标签:
2条回答
  • 2020-12-01 14:30

    For your future reference, the documentation is here:

    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    It says:

    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.

    0 讨论(0)
  • 2020-12-01 14:44

    Yes, it's safe if you don't modify the dictionary any more. Thread safety is only an issue in read/write scenarios

    0 讨论(0)
提交回复
热议问题