#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<semaphore.h>
#include<vector>
using namespace std;
int cnt = 0,N = 10,front = 0,rear = 0;
vector<int> buff(N);
pthread_mutex_t lock;
sem_t bin_sem;
pthread_cond_t empty,full;
void* proceducer(void* arg){
int i = 0;
do{
pthread_mutex_lock(&lock);
if(cnt>=N) pthread_cond_wait(&full,&lock);
cnt++;
cout<<"Proceducer: "<<rear<<endl;
rear = (rear+1)%N;
i++;
pthread_cond_signal(&empty);
pthread_mutex_unlock(&lock);
sleep(1);
}while(i<15);
return NULL;
}
void* consumer(void* arg){
int i = 0;
do{
pthread_mutex_lock(&lock);
if(cnt<=0) pthread_cond_wait(&empty,&lock);
cnt--;
cout<<"Consumer: "<<front<<endl;
front = (front+1)%N;
i++;
pthread_cond_signal(&full);
pthread_mutex_unlock(&lock);
// sleep(1);
}while(i<15);
return NULL;
}
int main(){
pthread_t threads_ID[2];
void* exit_status;
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&empty,NULL);
pthread_cond_init(&full,NULL);
pthread_create(&threads_ID[0],NULL,proceducer,NULL);
pthread_create(&threads_ID[1],NULL,consumer,NULL);
pthread_join(threads_ID[0],&exit_status);
pthread_join(threads_ID[1],&exit_status);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&empty);
pthread_cond_destroy(&full);
return 0;
}