Semaphore
Subject: Computer and Technology noun /ˈsɛm.ə.fɔːr/ 1 definition
- operating-systems
- concurrency-control
- process-synchronization
- thread-management
-
Definition (noun)
Entry #38 · UpdatedA Semaphore is a synchronization primitive that maintains an integer counter and a waiting queue to coordinate how multiple threads or processes access shared resources. Manipulated exclusively through two atomic operations—commonly called wait (decrement) and signal (increment)—it blocks callers when available permits reach zero and wakes queued waiters once a unit is freed. Unlike a mutex, a semaphore does not enforce thread ownership, allowing one execution context to acquire a permit and another to release it for both resource throttling and event signaling.
Common Types
Counting Semaphore: Uses an integer counter initialized to any non-negative value
Nto regulate concurrent access to a pool ofNidentical resources, such as database connections or worker slots.Binary Semaphore: Restricts its counter strictly to
0and1, acting as a lightweight signaling flag or basic lock where one execution context notifies another that an event has occurred or a single resource is free.Named Semaphore: Identified by a system-wide name (such as a POSIX path) rather than a memory address, allowing unrelated processes to open and synchronize on the same kernel-managed counter.
Unnamed Semaphore: Stored directly in shared memory without a filesystem identifier, providing low-overhead synchronization between threads within one process or related processes sharing a memory segment.
Example
A backend service maintains a pool of 10 persistent PostgreSQL connections guarded by a counting semaphore initialized to
10. When 25 concurrent API requests arrive simultaneously, the first 10 threads callwait()to decrement the counter and run their queries, while the remaining 15 block in the semaphore queue until an active request finishes and callssignal()to release a connection.