I need for a queue which can be accessed simultaneously by 2 threads, one can be enqueueing an item while a different one is dequeueing another item. Does the Queue<T> class meet this requirement? Or should I implement it from scratch (for example using a single producer/consumer circular queue implementation like this)?
I need for a queue which can be accessed simultaneously by 2 threads, one
Share
If you’re using
.NET 4.0you can use ConcurrentQueue. This is the recommended method.Regarding
Queue<T>itself, the MSDN page says the following (scroll down to theThread Safetysection:Besides these solutions, you can implement your own thread safe queue. One way to achieve thread safety is immutability (though this will involve some locking). You can read about writing an immutable queue in Eric Lippert’s blog, here. Alternatively, you can make use of the F#-based data types, most of which are immutable.