Spinlock versus Semaphore

后端 未结 11 754
广开言路
广开言路 2020-11-28 17:19

What are the basic differences between a semaphore & spin-lock?

When would we use a semaphore over a spin-lock?

11条回答
  •  难免孤独
    2020-11-28 17:42

    I would like to add my observations, more general and not very Linux-specific.

    Depending on the memory architecture and the processor capabilities, you might need a spin-lock in order to implement a semaphore on a multi-core or a multiprocessor system, because in such systems a race condition might occur when two or more threads/processes want to acquire a semaphore.

    Yes, if your memory architecture offers the locking of a memory section by one core/processor delaying all other accesses, and if your processors offers a test-and-set, you may implement a semaphore without a spin-lock (but very carefully!).

    However, as simple/cheap multi-core systems are designed (I'm working in embedded systems), not all memory architectures support such multi-core/multiprocessor features, only test-and-set or equivalent. Then an implementation could be as follows:

    • acquire the spin-lock (busy waiting)
    • try to acquire the semaphore
    • release the spin-lock
    • if the semaphore was not successfully acquired, suspend the current thread until the semaphore is released; otherwise continue with the critical section

    Releasing the semaphore would need to be implemented as follows:

    • acquire the spin-lock
    • release the semaphore
    • release the spin-lock

    Yes, and for simple binary semaphores on an OS-level it would be possible to use only a spin-lock as replacement. But only if the code-sections to be protected are really very small.

    As said before, if and when you implement your own OS, make sure to be careful. Debugging such errors is fun (my opinion, not shared by many), but mostly very tedious and difficult.

提交回复
热议问题