What is false wake spurious wakeup

Source: Internet
Author: User

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 (&notfull, &lock);

}

Makeing the product;

Pthread_cond_signal (&notempty);

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 (&notempty,&lock);

}

Consume a product;

Pthread_cond_signal (&notfull);

Pthread_cond_unlock (&lock);

}

return 0;

}

int main () {

pthread_t P,c;

void *retal;

Pthead_mutex_init (&lock,null);

Pthread_cond_init (&notempty,null);

Pthread_cond_init (&notfull,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 (&notempty);

Pthread_cond_destory (&notfull);

}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.