0

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)

BossRoss
  • 869
  • 2
  • 11
  • 31

1 Answers1

1

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.

Community
  • 1
  • 1
Alex
  • 13,024
  • 33
  • 62
  • I have used a ConcurrentDictionary. I needed to write an extension method so I could have the .ToConcurrentDictionary() functionality. – BossRoss Aug 31 '13 at 18:57
  • @BossRoss what would be the input for this extension method? Would it be similar to the current `ToDictionary` extension method? – Alex Aug 31 '13 at 19:45
  • 1
    [Here](http://stackoverflow.com/questions/12713857/extension-method-gets-no-overload-for-method-error) it is already answered :-) – BossRoss Aug 31 '13 at 19:48
  • @BossRoss I added a few extension methods to the above answer as well, that may perform a little better as the answer you linked to, as they do not rely on an intermediate conversion to a key value pair. They basically follow Linq's `ToDictionary` implementation, but insert into a concurrent dictionary instance instead of a regular one. – Alex Aug 31 '13 at 20:10
  • 1
    @Alex on second thought, it is better to add that extension method code as an answer to the original question. Removed again from this answer. – Alex Aug 31 '13 at 20:18
  • 1
    I can't edit it in, since it's too short, but you misspelled `ConcurrentDictionary` (you put an 's' in there). – Aske B. Sep 01 '16 at 13:56