Git Product home page Git Product logo

Comments (13)

sunwu51 avatar sunwu51 commented on May 18, 2024 4

futex的wait和wake过程,看起来和javaReentrantLock工作方式如出一辙,既然底层有了这样的实现了。jdk为什么还要再写一遍相同的过程,而且ReentrantLock在park和unpark的时候还会调用pthread中的pthread_cond_timewait最终还是通过futex系统调用。

from myblog.

ChangwangChen avatar ChangwangChen commented on May 18, 2024 1

futex的wait和wake过程,看起来和javaReentrantLock工作方式如出一辙,既然底层有了这样的实现了。jdk为什么还要再写一遍相同的过程,而且ReentrantLock在park和unpark的时候还会调用pthread中的pthread_cond_timewait最终还是通过futex系统调用。

我觉得还是用户态和内核态的区别吧

from myblog.

chen-shang avatar chen-shang commented on May 18, 2024

futex在真正将进程挂起之前会检查addr指向的地址的值是否等于val,如果不相等则会立即返回,由用户态继续trylock。否则将当期线程插入到一个队列中去,并挂起。

这一段有点没有懂,futex先检查addr指向的地址的值是否等于val,在到将当期线程插入到一个队列中去,并挂起。之间不是也有一个窗口么?如果检查addr指向的地址的值等于val,在放入队列并挂起之前有恰好有线程释放了锁,不是还是会有当前线程没有办法被唤醒的风险了么?

from myblog.

farmerjohngit avatar farmerjohngit commented on May 18, 2024

@chen-shang 下午的源码分析中有解释

  1. 加自旋锁
    2.检测*uaddr是否等于val,如果不相等则会立即返回
    3.将进程状态设置为TASK_INTERRUPTIBLE
    4.将当期进程插入到等待队列中
    5.释放自旋锁

其实就是通过自旋锁将检查addr将线程插入到等待队列放入到同一临界区

from myblog.

hanxuan123 avatar hanxuan123 commented on May 18, 2024

那对于那个有空窗期的代码,我们也在try_lock和wait之间加自旋锁不就没有空窗期了吗,还有一个问题就是这个futex和mutex有什么区别吗...比较是两个不同的api

from myblog.

heraldxy avatar heraldxy commented on May 18, 2024

是的,上面的空窗期,如果你能用一个volatile修饰的AtomicInteger当做标识符,那么就不存在空窗期了 @hanxuan123

from myblog.

WLWL avatar WLWL commented on May 18, 2024

谁来调用futex_wake方法呢?靠上层的线程通知notify吗?

from myblog.

withcancer avatar withcancer commented on May 18, 2024

唤醒的顺序是什么?看过其它地方有种说法是按线程优先级顺序来唤醒的,等待队列按照优先级排序

from myblog.

ChangwangChen avatar ChangwangChen commented on May 18, 2024

不知道作者的文章是能否随意转载, 发现了 这篇文章 提醒下是否侵权

from myblog.

afreefan avatar afreefan commented on May 18, 2024

是的,上面的空窗期,如果你能用一个volatile修饰的AtomicInteger当做标识符,那么就不存在空窗期了 @hanxuan123

volatile 不能保证原子性,两行代码不加锁 总会有空窗期的,这是我的理解

from myblog.

afreefan avatar afreefan commented on May 18, 2024

那对于那个有空窗期的代码,我们也在try_lock和wait之间加自旋锁不就没有空窗期了吗,还有一个问题就是这个futex和mutex有什么区别吗...比较是两个不同的api

try_lock是循环的条件 在循环while前后加锁解锁没有意义,如果在while前后加锁解锁 trylock要么一次成功,要么永远不成功

from myblog.

jiangchao-007 avatar jiangchao-007 commented on May 18, 2024

那么对于不同的方式申请的锁,比如共享内存创建的锁变量,在每个进程的虚拟地址是不同的。而线程组中的锁变量虚拟地址相同,当进行哈希的时候是怎么区分二者的呢?

from myblog.

zhangmuwuge avatar zhangmuwuge commented on May 18, 2024

所以说wait wake最底层还是用到了自旋锁,做最终资源的互斥

from myblog.

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.