本机互斥体实现 [英] Native mutex implementation

查看:81
本文介绍了本机互斥体实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我出世的日子里,我开始思考Windows/Linux是如何实现互斥体的,我已经以100种方式实现了这个同步器……在许多不同的架构中都采用了不同的方式,但从未想过它到底是如何实现的在大型ass操作系统中实现,例如在ARM世界中,我使一些同步器禁用了中断,但我始终尽管这样做并不是一个很好的方法.

So in my ilumination days, i started to think about how the hell do windows/linux implement the mutex, i've implemented this synchronizer in 100... different ways, in many diferent arquitectures but never think how it is really implemented in big ass OS, for example in the ARM world i made some of my synchronizers disabling the interrupts but i always though that it wasn't a really good way to do it.

我试图通过Linux内核游泳",但是就像我无法看到任何满足我好奇心的东西一样.我不是线程专家,但是我对线程的所有基本概念和中间概念都有扎实的了解. 有人知道互斥体是如何实现的吗?

I tried to "swim" throgh the linux kernel but just like a though i can't see nothing that satisfies my curiosity. I'm not an expert in threading, but i have solid all the basic and intermediate concepts of it. So does anyone know how a mutex is implemented?

推荐答案

在Windows世界中. Windows Vista Vista之前通过比较交换实现的互斥锁将互斥锁的状态从空"更改为正在使用",进入等待该互斥锁的CAS的其他线程将明显失败,必须将其添加到互斥锁队列中以进行进一步处理通知.队列的那些操作(添加/删除/检查)将受到Windows内核中的公共锁的保护. 在Windows XP之后,出于性能方面的考虑,互斥锁开始使用自旋锁,因为自锁对象是自足的.

In windows world. The mutex before the windows vista mas implemented with a Compare Exchange to change the state of the mutex from Empty to BeingUsed, the other threads that entered the wait on the mutex the CAS will obvious fail and it must be added to the mutex queue for furder notification. Those operations (add/remove/check) of the queue would be protected by an common lock in windows kernel. After Windows XP, the mutex started to use a spin lock for performance reasons being a self-suficiant object.

在Unix世界中,我没有那么生气,但可能与Windows 7非常相似.

In unix world i didn't get much furder but probably is very similar to the windows 7.

最后,对于在单个处理器上运行的内核,最好的方法是在进入关键部分时禁用中断,然后在退出时重新启用.

Finally for kernels that work on a single processor the best way is to disable the interrupts when entering the critical section and re-enabling then when exiting.

这篇关于本机互斥体实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆