#include<bits/stdc++.h>
#include<mutex>
#include<thread>
using namespace std;
mutex m;
condition_variable cv;
int fll= 0 ;
int empty= 0 ;
// using namespace std;
void producer( int i) {
unique_lock< mutex> lock( m) ;
while ( empty== 0 ) {
cv.wait ( lock) ;
}
cout << "producer " << i<< " has produced" << '\n ' ;
empty-- ;
fll++ ;
cv.notify_all ( ) ;
}
void consumer( int i) {
unique_lock< mutex> lock( m) ;
while ( fll== 0 ) {
cv.wait ( lock) ;
}
cout << "consumer " << i<< " has consumed" << '\n ' ;
empty++ ;
fll-- ;
cv.notify_all ( ) ;
}
int main( ) {
fll= 0 ;
empty= 5 ;
thread t1( consumer,1 ) ;
thread t2( consumer,2 ) ;
thread t3( consumer,3 ) ;
thread t4( consumer,4 ) ;
thread t5( consumer,5 ) ;
thread t6( consumer,6 ) ;
thread t7( consumer,7 ) ;
thread t8( producer,1 ) ;
thread t9( producer,2 ) ;
thread t10( producer,3 ) ;
thread t11( producer,4 ) ;
thread t12( producer,5 ) ;
thread t13( producer,6 ) ;
thread t14( producer,7 ) ;
// thread t15(producer,8);
thread t16( producer,9 ) ;
t1.join ( ) ;
t2.join ( ) ;
t3.join ( ) ;
t4.join ( ) ;
t5.join ( ) ;
t6.join ( ) ;
t7.join ( ) ;
t8.join ( ) ;
t9.join ( ) ;
t10.join ( ) ;
t11.join ( ) ;
t12.join ( ) ;
t13.join ( ) ;
t14.join ( ) ;
// t15.join();
t16.join ( ) ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGU8bXV0ZXg+CiNpbmNsdWRlPHRocmVhZD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbXV0ZXggbTsKY29uZGl0aW9uX3ZhcmlhYmxlIGN2OwppbnQgZmxsPTA7CmludCBlbXB0eT0wOwovLyB1c2luZyBuYW1lc3BhY2Ugc3RkOwp2b2lkIHByb2R1Y2VyKGludCBpKXsKCXVuaXF1ZV9sb2NrPG11dGV4PmxvY2sobSk7Cgl3aGlsZ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