Spin lock overview

Spin lock classes

The following class diagram shows the spin-lock and its associated semaphore:



The name "spin lock" is a bit of a misnomer because this lock does not actually spin. It uses the atomic Interlocked.CompareExchange operation to attempt to set itself as the lock owner and verify that it succeeded in a single atomic operation.

If it fails to acquire the lock immediately in this way then it blocks on a semaphore as a "heavy waiter". A count of threads that are blocked on the semaphore is maintained and is used to determine whether a release of the lock requires a release of the semaphore.

It is possible for the heavy waiter count to exceed the number of real waiters briefly as a thread attempts to take the lock and this can result in a thread being released from the semaphore prematurely. However, the released thread must still acquire the lock before it can proceed and so if the lock is not available then it will block again safely on the semaphore.

Last edited Aug 30, 2012 at 10:11 AM by jaorme, version 5


No comments yet.