I am using a Parallel.Foreach for populating an external ConcurrentBag. I tried also to use a common List and everything works fine.
I have been lucky or I missed the special scope of ConcurrentBag?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have been lucky;
Parallel.ForEachto populate a List is not thread-safe, you will eventually run into problems.According to MSDN,
List<T>is not thread safe:ConcurrentBag is what you should use for this, which is thread-safe for multiple readers and writers.