What is Semaphore?

A synchronization mechanism holding a counter of remaining units: decrement before use (waiting at 0) and increment when done. Used to manage resources with a fixed number of units.

Field: Multicore & Synchronization

Japanese name: セマフォ

The two operations

The semaphore is a synchronization mechanism devised by Dijkstra; the operation that decrements the counter is called P (wait) and the one that increments it V (signal or post). If the counter is 0 at a P operation, the thread is put in a wait queue and sleeps until another thread's V operation wakes it. Checking and changing the counter happen atomically, so the count stays correct even when several threads operate on it at once. A semaphore limited to 0 and 1 is a binary semaphore, and one that can hold any count is a counting semaphore. They are available through POSIX sem_wait and sem_post and through the APIs of many RTOSes.

How it differs from a mutex

A mutex has an owner, and only the thread that locked it can release it. A semaphore has no owner, and any thread can perform V, which makes it suitable for signalling events: an interrupt handler, for example, can use a semaphore to announce that data has arrived and wake the waiting task. On the other hand, when a binary semaphore is used as a lock, the owner is unknown, so in many implementations priority inheritance does not apply and priority inversion is harder to prevent. Mutexes are used for mutual exclusion, semaphores for counting and signalling.

Typical use and pitfalls

In the bounded-buffer producer-consumer problem, two counting semaphores, one for free slots and one for filled slots, are combined with a mutex that protects the buffer. If a thread takes the mutex first and then waits for a free slot, the other side can never get the mutex and the system deadlocks. Forgetting to perform V when leaving through an error path also leaves the available count permanently reduced. It is important to check the order of the waits and that every path releases what it took.

Try it: questions about Semaphore

What is the most suitable use of a counting semaphore?

  1. AAlways doubling the value of a variable
  2. BMerging all program execution into a single thread
  3. CLetting up to four threads at a time use a resource with a fixed number of units, such as four connections
Show answer and explanation

Answer: C. Letting up to four threads at a time use a resource with a fixed number of units, such as four connections

A semaphore holds a counter of remaining units: a thread decrements it before use (waiting if it is 0) and increments it when done. With an initial value of 4, up to four threads can use the resource at once. With 1, it works like a lock for mutual exclusion. Waiting threads are usually put to sleep and woken by the OS.

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary