views:

110

answers:

2

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?

+1  A: 

They will be present in .NET 4.0, I believe in the System.Threading.Collections namespace (or that's the namespace they were in in the last CTP I looked at).

Greg Beech
+3  A: 

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 Add or 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 in System.Collections, such as Hashtable, are thread-safe.

Now thread-safe Queue and Stack classes 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 see ConcurrentQueue and ConcurrentStack in .NET 4.0

Pavel Minaev
Ahh, yes. SyncRoot I believe.