
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 0.61 KB | hits: 15 | expires: Never
Producer and consumer always skips the same number
sem_init(&empty, 0, SIZE-1);
sem_init(&full, 0, 0);
sem_wait(&empty);
pthread_mutex_lock(&write);
//produce
pthread_mutex_unlock(&write);
sem_post(&full);
sem_wait(&full);
pthread_mutex_lock(&readers);
readCounter += 1;
if (readCounter != 3)
sem_post(&full);
pthread_mutex_unlock(&readers);
// read
pthread_mutex_lock(&readers);
readCounter -= 1;
if(readCounter == 0)
{
sem_post(&empty);
}
pthread_mutex_unlock(&readers);
sem_wait(&full);
pthread_mutex_lock(&readers);
// read
pthread_mutex_unlock(&readers);
// process item
sem_post(&empty);