What a semaphore is
A semaphore is a tool used in concurrent programming to control access to shared resources by multiple tasks. At its heart, a semaphore is a counter that keeps track of how many units of a resource are available. Tasks must ask the semaphore for permission before using the resource, and the semaphore grants or withholds access based on its count. This makes semaphores a flexible way to coordinate tasks that share limited resources.
How a semaphore works
A semaphore holds a count representing available resources. When a task wants to use a resource, it decrements the semaphore; if the count is above zero, the task proceeds, and if it is zero, the task waits. When a task finishes, it increments the semaphore, potentially letting a waiting task through. These two operations - waiting and signaling - are the core of how a semaphore regulates access, ensuring tasks take turns according to the available count.
Counting and binary semaphores
There are two common kinds. A counting semaphore can take a range of values and is used when a resource has several interchangeable units - for example, allowing a fixed number of tasks to use something at once. A binary semaphore has only two states, acting much like a simple lock that is either available or taken. The counting ability is what distinguishes the general semaphore from a basic one-at-a-time lock.
Semaphore versus mutex
Semaphores and mutexes are related but different. A mutex is specifically a lock meant to give one task at a time exclusive access, and typically the task that locks it is the one that unlocks it. A semaphore is more general: it can allow several tasks through at once, and its signaling can be used for coordination between different tasks. In short, a mutex is about exclusive ownership, while a semaphore is about counting and signaling available access.
What semaphores are used for
Semaphores are used whenever tasks must coordinate over shared resources. They can limit how many tasks access a resource pool at once, signal that an event has occurred so a waiting task can continue, or manage producers and consumers sharing a buffer. Their flexibility makes them a classic building block in operating systems and concurrent software, where controlling and coordinating simultaneous access is essential to correctness.
Why it matters
Semaphores are a foundational concept in concurrency, giving programmers a flexible way to coordinate tasks that share resources. Understanding what a semaphore is - a counter that controls and signals access to shared resources - clarifies how software manages many tasks running at once beyond simple locking. For anyone interested in how concurrent systems stay correct and coordinated, the semaphore is a key idea.
Related on Skillo
See also: What is a mutex? Explained simply, Process vs thread: the difference explained.
Sources
Published date reflects the original event date (2023-08-22). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Leave a Reply