Producer And Consumer Problem

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friend this is rupees and watching cbp nets video series on threading and this video is about producer consumer problem and if you know threading what is threading in c and c plus plus or any other language you should be knowing this particular problem producer consumer problem so in this video we'll learn about what is producer what is consumer and why it is called problem and this is very important when it comes to the interview because so many people ask this question in interviews you know i have asked the same question so many time and yeah the same problem is called bounded buffer problem so let's try to understand what is producer and consumer problem producer is also a thread and consumer is also a thread and you can visualize that producer is producing the data and consumer is consuming the data so they need a common buffer where they will do their operation meaning producer will produce the data and keep here so that consumer can take it from here now here is the problem okay this is a buffer yeah so the problem is see this is a common resource where this t1 thread and t2 thread are trying to access simultaneously this is a possibility that they will try to access this simultaneously and they will try to modify this buffer because producer is keeping the data meaning it will try to modify the state of the buffer and consumer is trying to remove the data so this is also going to modify the state of the buffer meaning they both are trying to access the same buffer at the same time then there is a race condition to avoid race condition we need a mutex let's have a mutex here now how it will work producer will try to acquire the lock let's suppose producer got the lock and it created the data let's suppose this one is the data now producer will notify to the consumer after producing this data it will notify this to the consumer that i have produced something and if you are waiting for this you go ahead and consume it and before notifying producer have to unlock the mutex so let me repeat this again producer took the lock produced the data meaning you put one data into the buffer now you're saying to the consumer that you can consume it but before telling that you have to release this mutex lock and yeah this notification will happen with the help of condition variable so we need a condition variable also i'll show you the program how it will work don't worry about that so consumer get notified consumer came here and took the data from here so now there is no data let's remove this once consumer have consumed the same story will go ahead consumer will this time notify to the producer dude i am done with the use and if you have something to produce go ahead so this is the cycle which will keep on happening until unless producer stops producing meaning let's suppose producer wanted to produce from zero to maybe one billion data it's just very small data then this consumer thread is always looking for the notification meaning someone have to wake up this consumer thread that okay the data have come start consuming so producer had this much of data producers started putting the data and started notifying to the consumer and consumer started consuming and notified back to the producer that if you have some more data please go ahead and produce it meaning produce some information and put into this buffer so that i can take it so this cycle will keep on going until unless producer run out of data and consumer will still wait for the notification so that if there are so many producer not only this let's suppose there is this is producer one maybe there can be n number of producers and they all are producing the data and only one thread which is consuming the data so maybe producer one is empty maybe producer two three are still going on so they will notify to the consumer and consumer will start consuming the data what is coming from producer two producer three and till producer n hey guys time for a quick pause and what you are seeing right now is my patreon page so if you don't know what is patreon it's a crowdfunding website where you can support any content creator like me and in return you get rewards so if you join me i can be your private tutor or you just want to chat with me and ask your doubts or maybe you just want to support me with very small amount and i'll still have something for you so do visit my patreon page and see if you like it and if you want to discontinue any time you can do that so if you already visited my patreon page let's continue our video now cool right so let's look at the program and from this video so this is the producer consumer program here it is fairly simple program what i have done is see this producer will take how much value it has to produce so i'll pass how many values it has to produce but consumer i'm not passing anything consumer will run infinitely so in while i have kept true here meaning this while loop will go on till the program is not terminated so it will run forever so consumer is running forever as i explained and producer is having some data to produce okay and the logic is see if i will give 10 here meaning if i am passing to the producer 10 then it will reduce till 1 and once it becomes 0 this while loop will terminate let's look at the driver function so this is your driver function see i have created t1 and t2 producer is taking 100 and consumer is running infinitely and i'm joining these two threads so that my main thread should not complete until unless these two threads are running so d1 got created producer got hundred let's see how it is working here we have hundred now we are using mutex and we are trying to acquire the log if we are successfully able to acquire the lock here which is this mutex and yeah this mutex should be common for producer and consumer i have seen so many people thinking that producer will have its own mutex and consumer will have its own mutex no dude it is not possible then there is no point of having a single key to open common place and then why are we guarding that place with a single key if multiple people can have the same key and go inside that correct then there should be only one key among all the threads if you want to synchronize all those threads for that particular data where you see race condition okay so let's assume it acquired the lock here and now see i am waiting on this condition variable not waiting i am checking a condition yeah there is should be a condition for producer and there should be a condition for consumer what is that condition the condition is see let's suppose your buffer size is maybe 100 or let's make it 50 for now and this is the size of your buffer you can at max put this much big number i mean these many data inside your buffer so now producer will have a duty to check before producing whether this buffer is almost full meaning if it is from 0 to 49 and the size of the buffer is 50 meaning i cannot produce it so it will wait for consumer to consume and remove the data from buffer so that it can produce it so before producing producer will check buffer is not full right yeah and similarly goes for the consumer consumer will check the buffer it should be having at least one data your buffer size should not be zero then there is no point of consuming the data so there are two conditions for producer if your buffer is full you will wait and for consumer if your buffer is empty you will wait so that's why i have kept this weight condition and on producer side it is checking that your buffer size is less than maximum buffer size okay so maximum buffer size i told right 50 so your buffer size currently your buffer size should be less than maximum buffer size so that you can produce something meaning you can push something in your buffer and let's suppose if this is not getting true you will wait at this line only by releasing this particular mutex and this happens automatically if you don't know what is the use of condition variable and mutex and all that you can go ahead and watch my videos i have explained it so nicely so let's assume we entered first time and this size is 0 meaning it is less than 50 we'll go inside this buffer dot push back this 10 will go into this buffer and then we are printing the message that okay produced 10 and now value is getting minus minus meaning 9 is the next value and then locker dot unlock and then we will notify to the consumer thread so we are saying that notify one we have another option we can say notify all if there are so many consumers but as we only one consumer will say notify one so as soon as you say notify one notify i mean consumer will get notified because see if you notice here consumer is also waiting for this condition variable and you are using the same condition variable to notify so that's how consumer will get notified so this time as you have notified consumer will wake up because consumer would have slept here because your buffer size was not greater than zero initially both came because we created t1 and t2 thread so they both started both thread started and here this guy will be able to get into the producer and will be able to produce but consumer will not be able to consume because initially your buffer is empty so both are coming here and both are checking this condition is getting true here but this condition is getting false so if it is getting false consumer is waiting but this one was able to produce so as i said now we got notified here and consumer will wake up and see okay buffer size is greater than zero yes because just now producer have produced one data now the buffer size is one one is greater than zero correct then we'll go ahead and remove the element from the buffer and remove and keep this value here and we are trying to print that value by saying that i have consumed it and the same thing i told you right we have to unlock this locker meaning this mutex and then we have to notify one we have to notify back to the producer dude you can start producing again because i have consumed it now there is a very big catch here i am telling you open your eyes open your mind okay this is not exactly how it is going to be because this is happening in loop the very first time producer produced it and unlocked locker condition variable was notified and this consumer was notified you know what is possible consumer is notified and just after that can you see this producer is going back in the loop and trying to reacquire the same lock what this consumer is also trying to lock now the possibility is after notifying to the consumer producer reacquired the log and it went to this condition and saw that okay size is still 1 because till that time consumer did not consumed it and this will become true it will come inside again and by that time consumer was trying to get the lock only and then consumer reached and saw that oh this lock is again acquired then it will again sleep for this log and this time value will become 8 and you would have printed 9 when you become 8 and you will do the same thing so this is the catch it looks like producer is producing one element and it is notifying to the consumer and then consumer is consuming that element no it is possible that in this loop producer is able to produce so many data and in between sometime consumer is able to acquire the mutex and then consumer will be able to consume n number of data and then producer will get the log back so it's not like one element and one element it can be multiple elements it depends on the cpu that which thread is able to get the lock fast it's just that simple so let me compile this compile successfully i'll execute it see let me expand this so how it started so from here we started produced 199 until 78 it was able to be in producer thread only meaning it was reacquiring the lock for these many time and just after that see now consumer get the lock c consumed consumed consumed consumed and then consumer till 100 it consumed successfully how much this producer was produced it consumed totally that many time and then after that producer got the lock because consumer was not having any data so it did not try to reacquire the lock it went for sleeping because of this condition this one because see it consumed 100 which was produced fast meaning there is no further data into the buffer so that's why this consumer thread was sleeping because of this size should be greater than 0 but size is 0. and then see producer produced the data but it could produce only four see this time it was able to produce only four data and then consumer was able to consume this four data and then again producers started producing and this time it was able to produce maybe seven one two three four five six seven yeah seven times and then consumer started and consumed for seven times so because of this while loop here there is a raise for this mutex who is able to get this mutex so quickly because both are running into the loop so this is how it will work and i'll show you this theory you can pause this video and see all these things it is just a problem statement is like producer will produce and consumer will consume with synchronization of a common buffer whatever i have explained these are there in the theory part and the important point is producer and consumer have to notify each other upon completion of their job this is very important job so once producer have produced something it will tell to the consumer that dude you can start consuming and the same thing will happen if consumer have consumed something it will tell you the producer that you can start producing so there is a synchronization they will do their job but they will notify each other then only this will have work and this is the actual producer consumer problem if you're not notifying each other then this is not a producer consumer problem so i hope you would have liked this video thanks for watching guys i'll see you in the next videos take care bye
Info
Channel: CppNuts
Views: 15,019
Rating: 4.9072466 out of 5
Keywords: programming, coding, producer consumer, producer and consumer, problem, producer consume problem, threading, introduction, locking, mutex, condition variable, c++, tutorial
Id: rKGq9AlfPR4
Channel Id: undefined
Length: 15min 41sec (941 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.