jargon

Comparison

MutexvsSemaphore

Mutex

you wrap the shared counter in a lock so only one thread can be inside that block at a time.

A primitive guaranteeing that only one thread executes a protected region at once. It is the simplest correct answer to shared mutable state and the easiest way to serialise your whole application by accident. Hold it for as little code as possible, and never across I/O.

Full entry →

Semaphore

you allow at most ten things through at once, not one, because the thing you are protecting is a capacity rather than a variable.

A counter-based primitive permitting up to N concurrent holders. It is the right tool for bounding concurrency against a limited resource — connections, in-flight calls to a downstream, memory-hungry jobs. A mutex is a semaphore of one, but reaching for a semaphore when you mean exclusivity makes the intent unclear.

Full entry →

Related comparisons