Semaphores and threads

Semaphores (sometimes referred to as counting semaphores) can be used to control access to shared resources. A semaphore can be thought of as an intelligent counter. Every semaphore has a current count, which is greater than or equal to 0.

Any thread can decrement the count to lock the semaphore (this is also called waiting on the semaphore). Attempting to decrement the count past 0 causes the thread that is calling to wait for another thread to unlock the semaphore.

Any thread can increment the count to unlock the semaphore (this is also called posting the semaphore). Posting a semaphore might wake up a waiting thread if there is one present.

In their simplest form (with an initial count of 1), semaphores can be thought of as a mutual exclusion (mutex). The important distinction between semaphores and mutexes is the concept of ownership. No ownership is associated with a semaphore. Unlike mutexes, it is possible for a thread that never waited for (locked) the semaphore to post (unlock) the semaphore. This can cause unpredictable application behavior. You must avoid this if possible.

The operating system provides the following additional capabilities of some semaphore APIs.
  • More complete management capabilities, including permissions on semaphores that are similar to file permissions
  • The ability to group semaphores in sets and perform atomic operations on the group
  • The ability to do multicount wait and post operations
  • The ability to wait for a semaphore to have a count of 0
  • The ability to undo operations that were done by another thread under certain conditions
Note: Java™ does not have the ability to use semaphores.