← Software Engineering & Cloud Studio
Concept Explainer · Software Engineering

Mutex vs. Semaphore

One is a lock with exactly one key and one owner. The other is a shared counter that any thread can nudge up or down.

Both a mutex and a semaphore are synchronization primitives, both get used to keep threads from stepping on each other, and in code they can look almost identical — acquire() on one side, release() on the other. But they encode two different ideas. A mutex is about ownership: it protects exactly one resource, and only the specific thread that locked it is allowed to unlock it. A semaphore is about counting: it has no concept of who did what, and any thread may increment or decrement it — which is exactly what you want when managing a pool of several interchangeable resources instead of exclusive access to a single one.