Team LiB
Previous Section Next Section

Spin Locks Versus Semaphores

Knowing when to use a spin lock versus a semaphore is important to writing optimal code. In many cases, however, there is little choice. Only a spin lock can be used in interrupt context, whereas only a semaphore can be held while a task sleeps. Table 9.6 reviews the requirements that dictate which lock to use.

Table 9.6. What to Use: Spin Locks Versus Semaphores

Requirement

Recommended Lock

Low overhead locking

Spin lock is preferred

Short lock hold time

Spin lock is preferred

Long lock hold time

Semaphore is preferred

Need to lock from interrupt context

Spin lock is required

Need to sleep while holding lock

Semaphore is required


    Team LiB
    Previous Section Next Section