I have implemented my own RingBuffer, but I’m surprised to see that .NET does not have one. Or even a simply thread safe queue, list or collection?
Why doesn’t .NET contain thread safe classes? Are they planned for the future?
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.
Thread safety of general purpose collection classes is of dubious value – more often than not, you don’t really need individual operations on them (such as
Addor the indexer) to be thread-safe, but you rather need group of operations to be thread-safe. For example, if one thread inserts new items into the collection, while another accesses items in a loop via indexer, it won’t matter if both of those methods on the collection will lock it – the code is still broken. That said, some old non-generic collection classes inSystem.Collections, such asHashtable, are thread-safe.Now thread-safe
QueueandStackclasses are actually useful (the limitations of their protocol make them suitable for general concurrent access), so their omission isn’t really by design, but more of an oversight (or, more likely, constrained resources). However, you’ll seeConcurrentQueueandConcurrentStackin .NET 4.0