Semaphore
PronunciationSEM-uh-for
Definition
A semaphore is a counter that controls how many threads or tasks can use a resource at the same time. Each user takes a permit and gives it back when done; if none are left, the next one waits.
Where you hear it
In concurrency code (limiting parallel downloads or database connections), OS courses and interviews.
Examples
A semaphore of 5 keeps us to five parallel requests.
Release the permit in a
finallyblock.A semaphore with a count of ten lets ten workers call the external API at the same time.
Common mistake
Not releasing the permit when an error happens. Slots leak until nothing can run.
Don't confuse with
A mutex, which allows exactly one holder at a time.
Say it at work
- In a meeting or chat
Limit it with a semaphore so we don't overload the API.
- In an email or ticket
What's the semaphore size?
Keep exploring
More in Programming Fundamentals