Goodbye semaphores?
Goodbye semaphores?
Posted Jan 5, 2006 7:48 UTC (Thu) by fenrus (guest, #31654)In reply to: Goodbye semaphores? by NightMonkey
Parent article: Goodbye semaphores?
To a large degree a mutex is sort of a sleeping spinlock. Eg it provides exclusion against other users of the lock, and the code pattern is "get lock; do stuff; release lock". (where "get lock" may sleep, unlike spinlocks).
Semaphores have a counter. They can be used in a mutexy way, but they can also be used for their counting property. Say you have 4 resources of something (say a piece of hardware). You start the counter at 4, and each kernel piece that uses the hardware first decrements (down) the count, and it increments (up) it back when it's done with the hardware. Semaphores sleep when the counter hits negative in down(), and the incremener up() wakes processes back up to be able to use the recently freed resource now.
By default, the counter is 1, and there is mutex-like behavior, but this is just a default and optional.
