Everyone Asleep
A one-slot bounded buffer with one producer and two consumers, sharing a single condition variable. Both waits are while loops, so a woken thread always rechecks before proceeding:
void *producer(void *arg) {
for (int i = 0; i < loops; i++) {
Mutex_lock(&mutex); // p1
while (count == 1) // p2
Cond_wait(&cond, &mutex); // p3
put(i); // p4
Cond_signal(&cond); // p5
Mutex_unlock(&mutex); // p6
}
}
void *consumer(void *arg) {
for (int i = 0; i < loops; i++) {
Mutex_lock(&mutex); // c1
while (count == 0) // c2
Cond_wait(&cond, &mutex); // c3
int tmp = get(); // c4
Cond_signal(&cond); // c5
Mutex_unlock(&mutex); // c6
}
}
The assertion inside get() never fires now, but the program still hangs, with all three threads asleep in Cond_wait. Which event puts it there?
Sign in to answer questions and track your progress
Sign In