3 ms·
I believe the atomic add is an optimization. Atomic add can be done with an instruction from user space. The semaphore wait needs to enter the kernel, which has
by jeffbush 12y ago
I believe the atomic add is an optimization. Atomic add can be done with an instruction from user space. The semaphore wait needs to enter the kernel, which has much more overhead.
By the way, as the author points out, we called this construct "Benaphores" in BeOS. Actually, semaphores were the primary kernel synchronization primitive in BeOS; there were no mutexes. Even sleep was implemented as a wait on a semaphore with a timeout. It made the kernel code cleaner because there was only one code path that unblocked threads. From the user space perspective, I found it to be pretty easy to use, especially since BeOS could acquire with a count greater than one. You could pretty easily to make a reader-writer lock with one semaphore (the writer acquired the max count)
The downside, as others pointed out, was that there was no way to do priority inheritance.