views:

86

answers:

1

Hello,

I have a Dictionary<> collection that contains characters. The collection has items added and removed constantly by multiple threads. Would initializing a new List<> collection using the dictionary need a lock?

Example code:

List<Character> charsToUpdate = new List<Character>(this.manager.characters.Values);

Thanks in advanced.

+7  A: 

Yes. When you construct a List<T> using this constructor, it enumerates the Dictionary. This isn't thread safe.

Make sure to synchronize (lock) the access to the dictionary for this, as well as the "numerous threads" adding and removing to the dictionary.

Reed Copsey