view raw
BossRoss BossRoss - 7 months ago 46
C# Question

Read/Remove items from dictionary in multi threaded environment

I have the following object:

Dictionary<string, List<SubmitSm>> smDic

which is a global variable.

What is the impact/implications of reading items from smDic into a local object for that thread and then remove the copied items from smDic while other threads are performing the same action? Each thread reading and removing items can only read items with a specific dictionary key(it is a one to one relationship)


i would advise you to use a System.Collections.Concurrent.ConcurrentDictionary in that case. It also implements the IDictionary interface and can be used in a thread safe manner for performing additions and deletions. See

A variant for ToDictionary for concurrent dictionaries as requested, that does not need intermediate conversion to a KeyValuePair can be found here: Extension method Gets "No overload for method" Error.