From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Overview of the producer-consumer collections

Overview of the producer-consumer collections - C# Tutorial

From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Overview of the producer-consumer collections

- [Instructor] In this chapter we'll look at these three collections. I've labeled these the Auto-Removal Collections because when we get an item from the ConcurrentQueue, the ConcurrentStack or the ConcurrentBag we get the one determined by the internal class algorithms and then that item is removed from the collection. These collections are useful by themselves but also serve as backing storage with a BlockingCollection class. In this chapter we'll look at using them individually. A quick review of the ConcurrentQueue and the other collections to see how they work with multiple threads. Use Enqueue to add items in a thread safe way to the ConcurrentQueue, in this example thread one has added two items to the queue. Other threads can safely add to the queue. Here thread two has added a third item. Later any thread can call TryDequeue to safely remove an item from the queue. Like all queues ConcurrentQueue uses a first-in,…

Contents