Explain what false awakening is?
It is easier to talk about specific examples.
pthread_mutex_t lock;
pthread_cond_t Notempty;
pthread_cond_t Notfull;
void *producer (void *data) {
for (int i = 0;i<=16;i++) {
Pthread_mtext_lock (&lock);
while (buffer if full) {
Pthread_cond_wait (¬full, &lock);
}
Makeing the product;
Pthread_cond_signal (¬empty);
Pthread_mutex_unlock (&lock);
}
return 0;
}
void *consumer (void *data) {
int i = 0;
while (1) {
Pthread_mutext_lock (&lock);
if (i++>16) break;
while (buffer is empty) {
Pthread_cond_wait (¬empty,&lock);
}
Consume a product;
Pthread_cond_signal (¬full);
Pthread_cond_unlock (&lock);
}
return 0;
}
int main () {
pthread_t P,c;
void *retal;
Pthead_mutex_init (&lock,null);
Pthread_cond_init (¬empty,null);
Pthread_cond_init (¬full,null);
Create thread
Pthread_create (&p,null,producer,0);
Pthread_create (&c,null,consumer,0);
Join
Pthread_join (C,&retval);
Pthread_join (C,&retval);
Destory
Pthread_mutex_destroy (&lock);
Pthread_cond_destory (¬empty);
Pthread_cond_destory (¬full);
}
Considering producer and consumer issues, for a producer thread, when it gets access to the buffer with exclusive access, it will determine if the buffer buffers are now full, if it is full.
If buffer buffers are now full, then he will block himself through the pthread_cond_wait () function, waiting for a position to be vacated.
Wait until the consumer thread consumes an element, using the pthread_cond_signal () function to inform the producer thread, "Empty position has, you can produce things."
Because multiple producer/consumer threads may share the same buffer buffers,
Then, when the p_a thread blocks to the wait function, the s_a thread consumes an element and uses the signal () function to notify the p_a wait () function,
There's another p_b. The thread gets the exclusive access to the buffer buffers, sees the vacated position, and does not block itself by the wait () function, producing things directly. But the original W
What is false wake spurious wakeup