Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Semaphores are often overused. The use of a semaphore to access a simple shared variable is overkill in most situations. The overhead involved in acquiring and releasing the semaphore consumes valuable CPU time. You can perform the job more efficiently by disabling and enabling interrupts, however there is an indirect cost to disabling interrupts: even higher priority tasks that do not share the specific resource are blocked from using the CPU. Suppose, for instance, that two tasks share a 32-bit integer variable. The ?rst first task increments the variable, while the second task clears it. When considering how long a processor takes to perform either operation, it is easy to see that a semaphore is not required to gain exclusive access to the variable. Each task simply needs to disable interrupts before performing its operation on the variable and enable interrupts when the operation is complete. A semaphore should be used if the variable is a ?oatingfloating-point variable and the microprocessor does not support hardware ?oatingfloating-point operations. In this case, the time involved in processing the ?oatingfloating-point variable may affect interrupt latency if interrupts are disabled.

...