Computer ScienceProgrammingMedium

Semaphore (concurrency)

Also known as:sync primitivecounting semaphorebinary semaphore

A semaphore is a synchronisation primitive used to control access to shared resources in concurrent programs by maintaining an integer count that represents the number of available permits. Two atomic operations define a semaphore: wait (P, decrement — block if zero) and signal (V, increment — wake a waiting thread). Introduced by Edsger Dijkstra in 1965, semaphores prevent race conditions and are foundational to operating system design, used for mutual exclusion (binary semaphore / mutex) and resource pool management (counting semaphore).

Binary Semaphore vs Counting Semaphore vs Mutex

PropertyBinary SemaphoreCounting SemaphoreMutex
Initial value0 or 10 to N1 (unlocked)
Max value1N (resource count)1
OwnershipNoneNoneOwning thread only
Use caseSignalling between threadsLimiting pool accessMutual exclusion
Release by non-owner?YesYesNo (undefined / error)

Interactive Tools

Codecademy – Operating Systems Concepts

Open Tool

Brilliant – Concurrency and Synchronisation

Open Tool

Khan Academy – Parallel Programming

Open Tool
Diagram showing two threads using a binary semaphore to synchronise access to a shared resource

Wikimedia Commons, CC BY-SA

Related Terms

From Greek "sema" (sign) + "phoros" (bearer). Edsger Dijkstra coined the computing term in his 1965 paper "Cooperating Sequential Processes", borrowing from railway semaphore signals that control train access to shared tracks.

semaphoresynchronisationconcurrencymutexrace-conditionoperating-systems