I have written a simple producer consumer using pthreads and semaphores. I am getting out of order(consumer consuming before producer has produced) output sometimes. Please help me in finding the problem . I have verified the logic using various sources and tutorials but still getting undesired results.
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <string>
#include <sstream>
#include <unistd.h>
#include "pthread_barrier.hpp"
sem_t empty;
sem_t full;
sem_t lock;
pthread_mutex_t wlock;
pthread_barrier_t pbarrier;
pthread_barrier_t cbarrier;
pthread_attr_t tattr;
#define BUFF_SIZE 100
volatile bool buffer[BUFF_SIZE];
int prodIterator = 0;
int consIterator = 0;
void *Producer(void *args)
{
pthread_barrier_wait(&pbarrier);
while(1) {
sem_wait(&empty);
sem_wait(&lock);
buffer[prodIterator] = true;
pthread_mutex_lock(&wlock);
std::stringstream str;
std::cout<<"producer produced = "<<prodIterator<<"\n";
pthread_mutex_unlock(&wlock);
prodIterator = (++prodIterator)% BUFF_SIZE;
sem_post(&lock);
sem_post(&full);
sleep(1);
}
}
void *Consumer(void *args)
{
pthread_barrier_wait(&cbarrier);
while(1) {
sem_wait(&full);
sem_wait(&lock);
buffer[consIterator] = false;
pthread_mutex_lock(&wlock);
std::cout<<"Consumer consumed = "<<consIterator<<"\n";
pthread_mutex_unlock(&wlock);
consIterator = (++consIterator)% BUFF_SIZE;
sem_post(&lock);
sem_post(&empty);
sleep(1);
}
}
int main()
{
sem_init(&empty, 0, BUFF_SIZE);
sem_init(&full, 0, 0);
sem_init(&lock, 0, 1);
pthread_mutex_init(&wlock, NULL);
pthread_t prod[10];
pthread_t cons[10];
unsigned pcount = 5;
unsigned ccount = 2;
pthread_barrier_init(&pbarrier, NULL, pcount);
pthread_barrier_init(&cbarrier, NULL, ccount);
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
for(int i=0;i<5;i++) {
pthread_create(&prod[i], &tattr, Producer, NULL);
}
for(int i=0;i<2;i++) {
pthread_create(&cons[i], &tattr, Consumer, NULL);
}
pthread_exit(NULL);
}
I have found out the problem. I am using mac osx and mac osx does not support unnmaed emaphores. It only supports named semaphores. So in place of using unnamed semaphores in my code, i needed to replace it with named semaphores to make the code work. OSX has very poor support for pthreads. It also does not support pthread barriers. So i needed to replace the semaphore init lines with following:
full = sem_open("/semaphore1", O_CREAT, 0777, 1);
empty = sem_open("/semaphore2", O_CREAT, 0777, 1);
lock = sem_open("/semaphore3", O_CREAT, 0777, 1);
and the declaration of semaphore variables needed be replace by :
sem_t *empty;
sem_t *full;
sem_t *lock;
very poor support indeed for pthreads in mac osx