且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

本机互斥体实现

更新时间:2023-11-13 22:32:34

在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.