美文网首页
虚假唤醒(spurious wakeup)

虚假唤醒(spurious wakeup)

作者: JaJIng | 来源:发表于2019-03-30 11:16 被阅读0次

On a multi-processor, it may be impossible for an implementation of pthread_cond_signal() to avoid the unblocking of more than one thread blocked on a condition variable.

The effect is that more than one thread can return from its call to pthread_cond_wait() or pthread_cond_timedwait() as a result of one call to pthread_cond_signal(). This effect is called “spurious wakeup”. Note that the situation is self-correcting in that the number of threads that are so awakened is finite; for example, the next thread to call pthread_cond_wait() after the sequence of events above blocks.

While this problem could be resolved, the loss of efficiency for a  fringe condition that occurs only rarely is unacceptable, especially given that one has to check the predicate associated with a condition variable anyway. Correcting this problem would unnecessarily reduce the degree of concurrency in this basic building block for all higher-level synchronization operations.

在多核处理器下,pthread_cond_signal可能会激活多于一个线程(阻塞在条件变量上的线程)。结果是,当一个线程调用pthread_cond_signal()后,多个调用pthread_cond_wait()或pthread_cond_timedwait()的线程返回。这种效应成为”虚假唤醒”(spurious wakeup)。

虽然虚假唤醒在pthread_cond_wait函数中可以解决,为了发生概率很低的情况而降低边缘条件(fringe condition)效率是不值得的,纠正这个问题会降低对所有基于它的所有更高级的同步操作的并发度。所以pthread_cond_wait的实现上没有去解它。

static void *thread_func(void *arg)

{

    while (1) {

      pthread_mutex_lock(&mtx);          //这个mutex主要是用来保证pthread_cond_wait的并发性

      while (msg_list.empty())  {    //pthread_cond_wait里的线程可能会被意外唤醒(虚假唤醒),如果这个时候,则不是我们想要的情况。

//这个时候,应该让线程继续进入pthread_cond_wait

          pthread_cond_wait(&cond, &mtx);

      }

      msg = msg_list.pop();

      pthread_mutex_unlock(&mtx);            //临界区数据操作完毕,释放互斥锁

      // handle msg

    }

    return 0;

}

相关文章

  • 虚假唤醒(spurious wakeup)

    On a multi-processor, it may be impossible for an impleme...

  • 记录一次用蓝牙键盘唤醒Ubuntu

    1. grep . /sys/bus/usb/devices/*/power/wakeup可以看到哪些设备可以唤醒...

  • Netty之线程唤醒wakeup [续]

    在之前的Netty之线程唤醒wakeup[https://www.jianshu.com/p/febcc077ce...

  • 网络唤醒-ether-wakeup

    网络唤醒功能就是实现在局域网中计算机能够接受特定的数据包实现开机的操作 原理:计算机在关机状态时,其实电源还提供给...

  • Netty之线程唤醒wakeup

    首先回顾下, Netty中的IO线程主要完成三件事1.轮询IO事件2.处理IO事件3.执行任务 在轮询IO事件的过...

  • 虚假唤醒

    原创文章,转载请注明原文章地址,谢谢! 生产者消费者案例 我们先用经典的生产者消费者案例来引出问题。 测试结果 通...

  • 线程虚假唤醒

    一般而言线程调用wait()方法后,需要其他线程调用notify,notifyAll方法后,线程才会从wait方法...

  • Argument

    causal relationship spurious relationship independent var...

  • RunLoop的wakeup port

    RunLoop的wakeup port起什么作用? 在查看RunLoop内容时,会显示wakeup port,如下...

  • 反序列化中__wakeup()函数漏洞

    __wakeup()是用在反序列化操作中。unserialize()会检查存在一个__wakeup()方法。如果存...

网友评论

      本文标题:虚假唤醒(spurious wakeup)

      本文链接:https://www.haomeiwen.com/subject/fypgvqtx.html