BossRoss
BossRoss

Reputation: 869

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)

Upvotes: 0

Views: 1216

Answers (1)

Alex
Alex

Reputation: 13244

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 http://msdn.microsoft.com/en-us/library/dd287191.aspx

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.

Upvotes: 1

Related Questions