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.

Understand the BlockingCollection

Understand the BlockingCollection

- [Instructor] The producer-consumer pattern is used to decouple processes that produce and consume data at different rates. For example, we might have some threads responsible for creating items and other threads responsible for consuming them. In most scenarios, we use a queue to store the items because the first item produced is usually the first item we want to consume. The BlockingCollection class is a good way to build a producer consuming system. It is built to handle common scenarios found in these types of systems. Here's an example of a producer-consumer scenario that will help visualize the problems and possible solution. We have a Producer adding items to the WorkItems queue. At the same time, the Consumer is processing the items as needed. Obviously the producers and consumers could work at different rates and rhythms. When the producer is adding items faster than can be processed by the consumer, the…

Contents