Producer Consumer Problem

Recently I had to analyze a number producer-consumer problem for the Logging framework.
This blog I'll be giving a brief introduction of the different ways in which we can do the Producer Consumer Problem in C#.

First of all what is the big deal with Producer Consumer Problem. Almost every programmer would come across scenarios where they have to implement this in some form. 

Producer - Some one that generates a Product
Consumer - Some one that consumes the Product.

Producer and Consumer will need to communicate about their work to each other.
Some way of synchronizing the DataCollection that will be shared between the Producer and Consumer.

Different ways the Producer and Consumer behave according to the requirement:

1. Producer creates a Product and then waits for the Consumer to consume the product. Consumer waits for the product and when the Producer creates the product consumes the product. Once the Product has been consumed the Producer can produce another Product. Till that time the Consumer waits for a new Product.

In the above scenario itself we can have a number of different ways:

A. The Producer and Consumer are sequential and will run on the same thread.
B. The Producer and Consumer are on different threads. 

2. Producer creates a limited number of Products and then waits for the Consumer to consume to the products. The MaxLimit of the queue or the DataCollection sets the Producer and Consumer to wait.

3. Producer keeps adding Products in the DataCollection whenever the Product is created. This is more important than the actual Consumption as thats a low priority task. Consumption takes more time and thus will be done on a different thread.

All the above scenarios will require certain ways of synchronization of the DataCollection which will be shared among the threads.

Next Part Series I will take each of the above scenarios and write some code in C#.

- Avinash