List<T> thread safety
问题 I am using the below code var processed = new List<Guid>(); Parallel.ForEach(items, item => { processed.Add(SomeProcessingFunc(item)); }); Is the above code thread safe? Is there a chance of processed list getting corrupted? Or should i use a lock before adding? var processed = new List<Guid>(); Parallel.ForEach(items, item => { lock(items.SyncRoot) processed.Add(SomeProcessingFunc(item)); }); thanks. 回答1: No! It is not safe at all, because processed.Add is not. You can do following: items