C
C#11mo ago
salty_pepper

What do use in a multi-threaded environment?Dictionary with lock OR ConcurrentDictionary?

Doesn't Dictionary with lock make ConcurrentDictionary redundant? As far as I understand to make dictionary thread safety we can just use lock to make sure other threads don't have access to the dictionary, why use ConcurrentDictionary ?
4 Replies
Cracker
Cracker11mo ago
ConcurrentDictionary manages thread safety better than just using lock on dictionary operations and you might forget to use lock or other developers so its better to make the collection thread safe if its crucial
Alen Alex
Alen Alex11mo ago
My answer would be, its a ready made, time tested solution for accessing dictionary in a concurrent way. Yes, if you need you can take the approach of manual locking, but for me, why reinventing the wheel, I would already have a lot of other logic to take care of
canton7
canton711mo ago
To be honest, ConcurrentDictionary provides some useful helper functions which aren't on normal Dictionary, so I'm sometimes tempted to reach for it even in non-concurrent situations
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?