A synchronization mechanism that is really just an integer that supports acquire and release.
If the count is > 0 and a thread tries to acquire the semaphore, the semaphore decrements the count and allows the thread to continue. Otherwise, the thread is blocked. When a thread releases, a blocked thread is woken up and the internal count is incremented.

  • This is saying if the semaphore is negative, the thread blocks itself and cannot continue until another thread increments it.
  • The OS provides an API to allow semaphore functionality

These are used to protect resources. If there is only one resource for example, you initialize the semaphore to 1.

There are 2 types:

  • Binary semaphore
  • Counting semaphore

Warning

These do not actually protect resources any more than a boolean would. The OS still needs to provide extra services to figure out what to do.