List thread safety

后端 未结 6 816
一生所求
一生所求 2020-11-28 12:33

I am using the below code

var processed = new List();
Parallel.ForEach(items, item => 
{
    processed.Add(SomeProcessingFunc(item));
});
         


        
6条回答
  •  旧巷少年郎
    2020-11-28 13:18

    From Jon Skeet's Book C# in Depth:

    As part of Parallel Extensions in .Net 4, there are several new collections in a new System.Collections.Concurrent namespace. These are designed to be safe in the face of concurrent operations from multiple threads, with relatively little locking.

    These include:

    • IProducerConsumerCollection
    • BlockingCollection
    • ConcurrentBag
    • ConcurrentQueue
    • ConcurrentStack
    • ConcurrentDictionary
    • and others

提交回复
热议问题