Click here to Skip to main content
15,038,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is safe to iterate over a generic concurrent collections on worker threads without locking on them (ConcurrentQueue, ConcurrentDictionary, ConcurrentStack, etc)?
Updated 1-May-14 7:28am
What's wrong about reading standard MDSN documentation on these types?
d.allen101 2-May-14 11:10am
I did read it. I understand that ConcurrentCollections lock on Adding and Removing Items but I don't understand why I throw an exception when I try to iterate over a ConcurrentCollection. If it's thread safe shouldn't it be locked while I'm performing an iteration, therefore throwing no Exception. I'm not understanding how it works. I know how to use it but I was trying to understand why and how it works. And I'm getting it wrong. Will you explain it to me SA? Thanks!

1 solution

Yes of course. Isn't that apparent from the documentation of these classes, even from their names? This is what they have been designed for. Moreover, such collections can server as a certain kind of thread synchronization utility for threads, because, say, they serialize data added to the same collection by different threads.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900