Everyone Ends Up Asleep
A bounded buffer holds one item. This version already uses while instead of if, so no thread acts on a stale condition:
cond_t cond; mutex_t mutex; int count = 0;
void *producer(void *arg) {
for (int i = 0; i < loops; i++) {
Mutex_lock(&mutex);
while (count == 1) Cond_wait(&cond, &mutex);
put(i);
Cond_signal(&cond);
Mutex_unlock(&mutex);
}
}
void *consumer(void *arg) {
for (int i = 0; i < loops; i++) {
Mutex_lock(&mutex);
while (count == 0) Cond_wait(&cond, &mutex);
int tmp = get();
Cond_signal(&cond);
Mutex_unlock(&mutex);
}
}
Run it with one producer and two consumers. What still goes wrong?
Sign in to answer questions and track your progress
Sign In