Giter Club home page Giter Club logo

Comments (6)

jamesge avatar jamesge commented on July 23, 2024

被wait的对象可能在wait返回后立刻析构,从而使析构和wake竞争。具体分析看这里的注释, futex已经处理了这种情况,会返回EFAULT(地址失效)或触发spurious wakeup(地址仍有效),butex中通过永不释放的内存池来解决问题,可能会触发spurious wakeup.

from brpc.

txsniper avatar txsniper commented on July 23, 2024

190行的注释中的例子我理解了,在这里我不大理解的是 futex_wait_private能够返回是由于另一个线程futex_wake_private调用之后(而不是pw->sig.store(SAFE_TO_DESTROY, butil::memory_order_release)调用之后),也就是不会出现futex_wake_private检查地址出现EFAULT(地址失效)的问题,难道是因为futex_wake_private还没结束, futex_wait_private就可以返回了(导致对象被销毁),导致futex_wake_private在继续执行时发现地址失效了?

from brpc.

jamesge avatar jamesge commented on July 23, 2024

这个是futex的典型现象了,当sig.store完成后,wait那边正好进入,发现sig和expected不相等,直接返回EWOULDBLOCK一路下去了,并不需要wake。

from brpc.

txsniper avatar txsniper commented on July 23, 2024

多谢解答,懂了。再问一个问题,在写多线程程序时,要将条件判断放到while循环中,就像下面这样:

while(条件不满足) {
      wait();
}

这是为了:

  1. 处理由于地址失效引起的spurious wakeup
  2. 多个等待线程被唤醒时,可能只有一个能够继续执行,其他的仍要wait等待

from brpc.

chenzhangyi avatar chenzhangyi commented on July 23, 2024

主要是1, futex以及signal(看EINTR的相关材料)可能引起spurious wakeup. 现代的操作系统都不会有惊群了.

from brpc.

txsniper avatar txsniper commented on July 23, 2024

@jamesge @chenzhangyi ,感谢二位的解答

from brpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.