是"benaphores"值得在现代OS上实施? [英] Are "benaphores" worth implementing on modern OS's?

查看:31
本文介绍了是"benaphores"值得在现代OS上实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回到我作为BeOS程序员的那一天,我读了

Back in my days as a BeOS programmer, I read this article by Benoit Schillings, describing how to create a "benaphore": a method of using atomic variable to enforce a critical section that avoids the need acquire/release a mutex in the common (no-contention) case.

我认为这很聪明,而且似乎可以在支持原子增量/减量的任何平台上做同样的事情.

I thought that was rather clever, and it seems like you could do the same trick on any platform that supports atomic-increment/decrement.

另一方面,这看起来很容易包含在标准互斥量实现本身中……在这种情况下,在我的程序中实现此逻辑将是多余的,不会带来任何好处.

On the other hand, this looks like something that could just as easily be included in the standard mutex implementation itself... in which case implementing this logic in my program would be redundant and wouldn't provide any benefit.

有人知道现代的锁定API(例如pthread_mutex_lock()/pthread_mutex_unlock())是否在内部使用此技巧吗?如果没有,为什么不呢?

Does anyone know if modern locking APIs (e.g. pthread_mutex_lock()/pthread_mutex_unlock()) use this trick internally? And if not, why not?

推荐答案

您的文章所描述的内容现已广泛使用.通常,它被称为"关键部分",它由一个互锁的变量,一堆标志和一个内部同步对象(如果我没有记错的话,是Mutex)组成.通常,在争用很少的情况下,关键部分完全在用户模式下执行,而不涉及内核同步对象.这保证了快速执行.当争用较高时,将使用内核对象等待,从而释放传导时间的时间片以加快周转时间.

What your article describes is in common use today. Most often it's called "Critical Section", and it consists of an interlocked variable, a bunch of flags and an internal synchronization object (Mutex, if I remember correctly). Generally, in the scenarios with little contention, the Critical Section executes entirely in user mode, without involving the kernel synchronization object. This guarantees fast execution. When the contention is high, the kernel object is used for waiting, which releases the time slice conductive for faster turnaround.

通常,在当今时代,实现同步原语几乎没有意义.操作系统带有大量这样的对象,并且它们在比单个程序员所想象的要大得多的场景中进行了优化和测试.从字面上看,发明,实施和测试良好的同步机制需要花费数年的时间.这并不是说尝试没有任何价值:)

Generally, there is very little sense in implementing synchronization primitives in this day and age. Operating systems come with a big variety of such objects, and they are optimized and tested in significantly wider range of scenarios than a single programmer can imagine. It literally takes years to invent, implement and test a good synchronization mechanism. That's not to say that there is no value in trying :)

这篇关于是"benaphores"值得在现代OS上实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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