A semaphore is a signaling mechanism. In computing, it’s a variable that controls access to shared resources when multiple processes or threads run at the same time. Outside of computing, the word refers to visual signaling systems, from flag positions used by sailors to mechanical arms on railway tracks. The concept is the same across all these uses: communicate a status so that others know whether to proceed or wait.
Semaphores in Computing
In computer science, a semaphore is an integer variable that coordinates how multiple threads or processes share a resource. Think of it like a counter that tracks how many “access slots” are available. When a thread wants to use a shared resource, it checks the semaphore. If the count is above zero, the thread proceeds and the count drops by one. If the count is already at zero, the thread waits in a queue until a slot opens up.
Two operations make this work, and they’re designed to be atomic, meaning nothing can interrupt them halfway through. The first operation (called “wait” or “P”) decreases the semaphore’s value and blocks the thread if the value hits zero. The second operation (called “signal” or “V”) increases the value by one and wakes up a waiting thread if there is one. The letters P and V come from the Dutch computer scientist Edsger Dijkstra, who invented semaphores in the 1960s. P stands for “proberen” (to test) and V for “verhogen” (to increment).
Semaphores solve three core problems. They prevent two threads from modifying the same data at the same time, which would cause unpredictable results (known as a race condition). They control access to a limited pool of resources, like database connections. And they let one thread pause and wait until another thread finishes a specific task before continuing.
Binary vs. Counting Semaphores
A binary semaphore toggles between 0 and 1. It acts like a simple lock: one thread enters, and everyone else waits. Only one entity can access the protected resource at a time, which enforces mutual exclusion. If a thread signals a binary semaphore that already has a value of 1, nothing changes.
A counting semaphore ranges from 0 to some higher number, say 10. This allows multiple threads to access a resource simultaneously, up to that limit. A common example: if you have a pool of five database connections, you’d initialize the semaphore to 5. Each thread that grabs a connection decrements the count. When all five are taken, the next thread waits. When a connection is returned, the count goes back up and a waiting thread can proceed. Signaling a counting semaphore that already has tokens available simply adds another, increasing the value to the next number.
How Semaphores Differ From Mutexes
Mutexes and binary semaphores look similar on the surface, but they differ in one important way: ownership. A mutex can only be unlocked by the same thread that locked it. A semaphore has no such restriction. Any thread can call the signal operation, even if it wasn’t the one that called wait. This makes semaphores more flexible for coordinating between threads, but it also means they lack some safety features.
Mutexes include a mechanism called priority inheritance, which helps reduce a problem where a low-priority thread holds a resource that a high-priority thread needs, causing the high-priority thread to stall. Semaphores don’t have this protection. In practice, you’d use a mutex when you need strict one-at-a-time access with clear ownership, and a semaphore when you need to manage a pool of resources or coordinate timing between threads.
Using Semaphores in Code
On Linux and Unix-like systems, the POSIX standard provides a semaphore library. You create a semaphore with an initial value, then use two main functions to operate on it. One function decrements the value (and blocks if it’s already zero). The other increments the value (and wakes a waiting thread if one exists). Named semaphores can be shared between entirely separate programs by referencing the same name, while unnamed semaphores are typically shared between threads within a single program. When you’re done, you destroy or unlink the semaphore to clean up system resources.
The Original Semaphore: Optical Telegraphs
The word “semaphore” predates computers by about 170 years. In the 1790s, French engineer Claude Chappe built a network of towers equipped with large movable wooden arms and telescopes. Each tower could see the next one in the chain, and operators would arrange the arms into positions representing letters, words, or numbers. The receiving tower would copy the position and pass it along.
Chappe’s first line of 15 towers connected Paris to Lille, roughly 120 miles apart. In August 1794, the system transmitted news that the French Republican army had recaptured a town from the Austrians in less than an hour. The same message by horseback courier would have taken about 24 hours. This was, for its time, a revolutionary leap in communication speed.
Flag Semaphore
Flag semaphore is a visual signaling system where a person holds two flags and positions their arms at specific angles to represent letters and numbers. Each character uses a unique combination of left-hand and right-hand positions: down, low (45 degrees below horizontal), out (horizontal), high (45 degrees above horizontal), or up (straight overhead). The letter A and the number 1 share the same position, with the left hand down and the right hand at the low position. Letters B through G and numbers 2 through 7 follow a pattern of keeping one hand down while rotating the other. A special “numerical sign” position tells the receiver that the following signals represent numbers rather than letters.
Flag semaphore was widely used by navies for ship-to-ship communication before radio became standard. It remains part of naval training and is occasionally used as a backup signaling method.
Railway Semaphore Signals
Railway semaphores use a pivoted mechanical arm mounted on a tall post to tell train drivers whether to stop or proceed. When the arm is horizontal, it means stop. When tilted to 45 degrees, it means the track is clear. At night, colored lights correspond to the arm position: red when horizontal (stop), green when tilted (proceed), and yellow for distant warning signals.
These mechanical signals have been largely replaced by electronic and LED signaling systems. In the UK, semaphore signals still operate on some secondary rail routes as of 2024, though they’re disappearing faster as upgrades roll out. In North America, mechanical signals are nearly extinct on main lines. One of the last significant installations was on BNSF Railway’s route through Glorieta Pass in New Mexico, used by Amtrak’s Southwest Chief.
Why the Same Word Covers All of These
Whether it’s a wooden arm on a French tower, a flag held at shoulder height, or an integer variable in an operating system, a semaphore communicates state. The optical telegraph told distant observers whether a message was ready. The railway arm tells a driver whether to stop or go. The computing semaphore tells a thread whether a resource is available. Dijkstra borrowed the term deliberately: his software mechanism works on the same principle that Chappe’s towers did two centuries earlier. Something is either available or it isn’t, and the semaphore is how everyone agrees on which it is.

