Skip to content

Instantly share code, notes, and snippets.

@kmjayadeep
Created November 28, 2016 16:05
Show Gist options
  • Save kmjayadeep/4e7492d4a3372af6bc3826fa1508a46a to your computer and use it in GitHub Desktop.
Save kmjayadeep/4e7492d4a3372af6bc3826fa1508a46a to your computer and use it in GitHub Desktop.
Producer Consumer problem implementation in cpp
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <random>
#include <unistd.h>
using namespace std;
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE];
int index=0;
sem_t full,empty;
pthread_mutex_t mutex;
void* produce(void* arg){
while(1){
sleep(1);
sem_wait(&empty);
pthread_mutex_lock(&mutex);
int item = rand()%100;
buffer[index++] = item;
cout<<"Produced "<<item<<endl;
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void* consume(void* arg){
while(1){
sleep(1);
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[--index];
cout<<"Consumed "<<item<<endl;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main(){
pthread_t producer,consumer;
sem_init(&empty,0,BUFFER_SIZE);
sem_init(&full,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_create(&producer,NULL,produce,NULL);
pthread_create(&consumer,NULL,consume,NULL);
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment