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;
}