Skip to main content
CSOS & ConcurrencyDiagramIntermediate

Mutex vs Semaphore

KO: 뮤텍스 vs 세마포어·EN: Mutex vs Semaphore·JA: ミューテックス vs セマフォ

Comparing Mutex (single-owner lock for mutual exclusion) against Semaphore (ownerless permit counter for bounded concurrency and signaling)

Table of Contents

Structure Diagram

Mutex vs Semaphore Synchronization Comparison
MUTEXMutex (Mutual Exclusion)

Ownership-based lock · 1 thread at a time

  • ConcurrencyStrictly 1 thread (mutual exclusion)
  • OwnershipOnly the locking thread can unlock
  • Priority InversionSupports Priority Inheritance
  • Primary UseProtecting shared state critical sections
SEMAPHORESemaphore

Counter-based N permits · Pool & signaling

  • ConcurrencyUp to N concurrent permits
  • OwnershipNo ownership (any thread can signal)
  • OperationsWait/Acquire (-1) & Signal/Release (+1)
  • Primary UseConnection pools, rate throttling, signaling
Quick Answer

What is Mutex vs Semaphore?

Comparing Mutex (single-owner lock for mutual exclusion) against Semaphore (ownerless permit counter for bounded concurrency and signaling). It is a core concept in OS & Concurrency within CS.

Core Principles

Both Mutexes and Semaphores coordinate concurrent access, yet they differ fundamentally in ownership and intent.

  • Mutex: Grants exclusive access to one thread at a time and enforces ownership—only the thread that locked it may unlock it.
  • Semaphore: Maintains an integer permit counter N without ownership. Threads decrement on acquire (wait) and increment on release (signal), making it ideal for bounding concurrency or cross-thread signaling.

Practical Takeaways

  • Use a Mutex (not a binary semaphore) for critical sections: Ownership prevents accidental cross-thread unlocks and enables OS priority-inheritance protections.
  • Upgrade to RWLock for read-heavy state: Allows concurrent readers while serializing writers.
  • Always use scoped guards: Wrap locks in RAII / defer / try-finally so exceptions never leave a lock permanently held.

Notable Examples

  • Mutex: Protecting shared maps, memory allocators, and log writers.
  • Semaphore: Bounding DB connection pools, capping concurrent outbound HTTP calls, and producer-consumer slot tracking.