Producer and Consumer Problem waiting threads

890 views Asked by At

I tried to code Producer and Consumer Problem but after producing up to a certain value -(val) produce thread ends. but the consumer thread's condition variable waiting for notification but there is no thread to notify so my program does not end. What changes should I made?

#include<bits/stdc++.h>
using namespace std;
mutex m;
vector<int>buffer;
condition_variable cv;
void producer(int val){
    while(val){
        std::unique_lock<mutex>lock(m);
        cv.wait(lock, []{ return buffer.size()<50; });
        buffer.push_back(val--);
        cout<<"Produced : "<<buffer.back()<<endl;
        lock.unlock();
        cv.notify_one();
      }
}
void consumer(){
    while(1){
        std::unique_lock<mutex>lock(m);
        cv.wait(lock, []{return buffer.size() > 0 ; });
        cout<<"Consumed : "<<buffer.back()<<endl;
        buffer.pop_back();
        lock.unlock();
        cv.notify_one();
    }
}

int main()
{
    int attempt=1;
     thread t1(producer,30);
    thread t2(consumer);
  
    t1.join();
     t2.join();
 

    return 0;

}

1

There are 1 answers

5
ixSci On

You need to have another variable which will indicate that your producer will produce no more so your consumer will know that

  • it doesn't need to wait for the producer anymore.
  • if it consumed the latest item it should stop the loop.

So the minimum modification of your code might look as follows:

using namespace std;
mutex m;
vector<int>buffer;
condition_variable cv;
bool producerExhausted = false;
void producer(int val){
    while(val){
        std::unique_lock<mutex>lock(m);
        cv.wait(lock, []{ return buffer.size()<50; });
        buffer.push_back(val--);
        cout<<"Produced : "<<buffer.back()<<endl;
        producerExhausted = val == 0;
        lock.unlock();
        cv.notify_one();
    }
}
void consumer(){
    while(1){
        std::unique_lock<mutex>lock(m);
        if(!producerExhausted)
            cv.wait(lock, []{return buffer.size() > 0 ; });
        cout<<"Consumed : "<<buffer.back()<<endl;
        buffer.pop_back();
        if(producerExhausted && buffer.empty())
            break;
        lock.unlock();
        cv.notify_one();
    }
}