What is spin lock in operating system ?

  • 0

    Spin locks are a low-level synchronization mechanism suitable primarily for use on shared memory multiprocessors. When the calling thread requests a spin lock that is already held by another thread, the second thread spins in a loop to test if the lock has become available. When the lock is obtained, it should be held only for a short time, as the spinning wastes processor cycles. Callers should unlock spin locks before calling sleep operations to enable other threads to obtain the lock

  • 2

    Spin lock is a lock that will cause a thread enter a loop to repeatedly check if the lock is available.

    It is different from regular lock like mutex which will put a thread into BLOCK state and likely to preempt the thread, which has more overhead when the lock is only unavailable for a short period of time. If the thread could acquire the lock within its own CPU quota, then it would be more efficient to use spin lock. However, if the waiting time is unknown, or too long, mutex will be definitely better.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.