Concurrency Comparisons
Side-by-side trade-offs where neither column wins. The workload, the runtime and how much correctness risk you can carry decide — and each comparison ends with the verdict that follows from that, not from a preference.
Concurrency vs parallelismThreads vs processesAsync vs threadsEvent loop vs thread poolMutex vs semaphoreOptimistic vs pessimistic concurrency controlLock-based vs lock-freeBounded vs unbounded queue
Mutex vs semaphore
They look similar and are used for opposite purposes. A mutex protects an invariant; a semaphore limits a resource. Using one for the other's job is a recognizable code smell with a recognizable failure.
| Dimension | Mutex | Semaphore |
|---|---|---|
| What it expresses | "Only one task may be inside this region" | "At most N tasks may be doing this at once" |
| What it protects | An invariant over shared state | A limited resource or a rate of work |
| Ownership | Owned by the locker — usually only it may unlock | No ownership; any task may release a permit |
| Count | One, always | N, chosen by you |
| Typical scope | A few lines, no I/O inside | A whole operation, including I/O |
| Signature failure | Deadlock from a second lock taken inside | Permit leaked on an error path — capacity silently drops to zero |
| Reentrancy | Non-reentrant by default; re-locking self-deadlocks | Not applicable — taking two permits is legitimate |
| Right question | "What must stay true?" | "How many at once, and why that number?" |
Use Mutex when
- A multi-field update must never be observed half-done.
- The region is short and contains no I/O.
- The correct answer to "how many at once" is exactly one.
Use Semaphore when
- Capping in-flight requests to a downstream service.
- Bounding memory by bounding how many large buffers exist at once.
- Gating access to a fixed pool of connections or file handles.
Verdict
If you are protecting correctness, it is a mutex. If you are protecting capacity, it is a semaphore. A semaphore with one permit used as a mutex loses ownership checks and gains a way to be released by the wrong task.