So, guys, now in this lecture video, we will going to implement producer consumer problem statement.
So all the codes that is related to producer consumer can be found in this code directory path and the
file which represents an assignment into which we will going to write a code is this file that is assignment
producer consumer on Q dot C.
Right?
So since the producer consumer are the threads which needs to act on a common resource, that particular
resource is nothing.
But we have taken that as a queue.
So the queue data structure is implemented by these source file that is Q, dot C and Q dot H.
We will going to use these files in the assignment and compile Dot as such is a shell script that compiles
your entire assignment and create an executable exe.
You simply need to run this executable in order to test your code.
Right?
So this is the assignment file into which we will going to write a code.
So now let us understand the problem statement in little bit detail.
So what we will going to do is that in our program we will have our resource which is represented by
a data structure.
Q Right, so this is the common resource which needs to be accessed by producer and consumer threads.
We will going to create a program in which there will be two consumer threads.
The name of the two consumer thread is Tc1 and Tc2, right?
And as the name suggests, these are consumer threads, meaning that both these threads has the objective
to remove an element from the queue until the queue is empty.
Right.
Then in our program we will going to launch two more threads which are called producer threads.
So the name of the threads is tp1 and tp2.
And the objective of the producer threads is to create an element and push it into the queue until the
queue is full.
Right?
So you can see the job of the producer and consumer threads is exactly opposite to each other.
Then queue is a common data structure which is being acted upon by consumer and producer threads.
Therefore queue has condition variable and a mutex.
Right.
We already know that mutex is always a property of the resource that is being shared by the threads
and condition variable we have taken in this case as a property of the queue.
Right.
All the threads which are competing to get an access over the shared resource queue will be blocked
on this condition variable if the queue is not available.
Right.
So if you happen to take a look at the implementation of this queue, I will show you that while you
open the file queue.
Dot h.
You will see that this is the data structure which represents the queue.
And this data structure has a mutex and a condition variable.
Right.
And note that the default size of the queue is five, meaning that the maximum size of the queue cannot
grow beyond five.
We will going to discuss how to make use of this queue library in our program in order to implement
our producer consumer problem statement.
So now we know that in our program we need to launch two consumer threads and two producer threads and
we have a common data source which is queue, which all the four threads would compete to get an access,
right?
These slides, which represents the problem statement have been attached in the resource section so
that you can refer to the problem statement and attempt the solution by your own.
Now let us discuss what exactly the problem statement is.
So we will going to write a program which launches four threads to consumer threads and two producer
threads.
And you may create these threads in the joinable mode.
Right now, all these four threads act on a shared resource which is nothing but a queue of integers.
So you need to create a queue which store the integers as an element.
Producer thread produce a random integer and add it to a queue.
Whereas consumer threads remove an integer from the queue.
Right.
And the maximum size of the queue is five.
This we have already discussed.
So this is the overview of the problem statement that we will going to resolve.
And also our problem statement has certain constraints which you need to implement or you need to respect.
So while writing a solution, you need to implement the following constraints as well.
The first constraint says that when produce a thread, produce an element and add it to the queue.
The producer thread does not release the queue until the queue is full.
That is, producer thread releases the queue only when the queue is full.
Right.
So producer thread keep on adding element to the queue until the queue is full.
Once the queue is full only then the producer thread actually releases the queue.
Similarly, when consumer thread consume an element from the queue, it consumes the entire queue and
do not release the queue until the queue is empty.
Right.
And the point number three says that consumer thread signals the producer thread when the queue is exhausted
and producer thread signals the consumer thread when the queue is full.
So you can see that there is bidirectional communication between consumer threads and producer threads.
The consumer thread signals the producers, whereas producer thread signals the consumers right.
And while implementing the solution, please use the following guideline.
Use as many printers as possible so that you can debug the program easily.
The program, which involves multithreading are hard to debug using debugging tools such as GDB or et
cetera.
Print f should be extensively used in your program so that if your program encounter a deadlock like
situation, you can very well analyze the logs or the sequence of events taking place from the print
f output.
And you would figure it out that what exactly the problem is.
Right?
And going further, since in our program we need to make use of the data structure.
So Q dot is the header file and this header file will give you the access to the following APIs, right?
You can see on the right hand side that you can make use of the Q by using the following APIs in this
way.
So this is the example usage regarding how you can make use of the Q in your program.
So for example, if you want to create a new Q, you can simply use the API init Q and it will give
you a pointer to the Q which has been initialized.
Similarly, if you need to check whether the Q is empty, you can use this API and in order to enqueue
an integer element into the Q, you can simply use the Q API, right?
So in this example we are simply queuing up the element five in the queue.
And in case if you remove an element from the queue, you can use an API.
Q Right, it will remove an element from the queue and it will give you an integer value.
Similarly, if you need to find how many elements are present in the queue, you can use this queue
underscore count.
Right.
And this is another is queue full which will tell you whether your queue is full or not.
So while writing the solution which implements producer consumer problem statement that we have just
discussed, you need to make use of these APIs on the queue data structure, right?
So now in the next lecture video, let us set up our homework problem to begin with and then we will
discuss how to write producer and consumer algorithms in order to implement the producer consumer problem
statement that we have discussed and also respect all the constraints that we have discussed.