Glossary · Automation software engineering and architecture
Semaphore
Also known as: Counting semaphore, Binary semaphore
German: Semaphor
In concurrent programming, a semaphore is a synchronization object holding a counter that tasks decrement to acquire and increment to release, used to limit access to a resource pool or to signal events between tasks.
- Software engineering
In one sentence
A semaphore is a synchronization counter that tasks decrement and increment to limit access to resources or signal events.
Example
An interrupt service routine gives a semaphore when a CAN message arrives, and the waiting communication task takes it and processes the message.
How it applies
- Engineering: Counting semaphores manage N identical resources; binary semaphores signal events, for example from an interrupt to a task. Because semaphores have no owner, they generally do not support Priority inheritance.
- Design: For protecting shared data between tasks, a Mutex is usually the better choice. Using semaphores for mutual exclusion risks priority inversion.
- Documentation: Software design documents should describe which semaphores exist, what they signal or count, and which tasks give and take them.
Semaphore vs. mutex
A Mutex has an owner, allows one holder and is meant for mutual exclusion. A semaphore has no owner, can allow several holders and is often used for signaling.