Mutex vs Semaphore: Differences Explained

A mutex protects a critical section so that only one thread at a time can use a shared resource, and the thread that locked it must be the one that unlocks it. A semaphore is a counter: it controls how many threads can use a limited resource, and it can also be used to signal events from one thread to another. A mutex is about ownership and exclusion; a semaphore is about counting and signaling.

AspectMutexSemaphore
PurposeMutual exclusion: one thread at a time in a critical sectionCounting: limit access to a pool of N resources, or signal events between threads
ValueLocked or unlocked (binary)A non-negative integer count (a binary semaphore is the special case of 0 or 1)
OwnershipOwned by the thread that locked it, and that thread should unlock itNo owner: any thread can post or signal
OperationsLock and unlockWait (P, decrement) and post (V, increment)
Signaling between threadsNot intended for itYes: for example a producer signaling a consumer
Priority inversionOften avoided with priority inheritanceUsually not supported
Typical usesProtecting a shared data structure such as a list or a counterLimiting a connection pool, bounded producer-consumer buffers, signaling from an interrupt handler to a task
Common pitfallsDeadlock from inconsistent lock order, or forgetting to unlockLost signals and deadlock when counts do not match

Figures are typical orders of magnitude and vary by process generation and product.

When to use which

Mutex
When several threads touch the same data and only one may do so at a time.
Semaphore
When you need to limit how many threads use a resource, or to wake a thread when something happens.

FAQ

Is a mutex the same as a binary semaphore?

Not quite. Both have two states, but a mutex has an owner that must unlock it and often supports priority inheritance, while a semaphore has no owner and can be signaled by any thread.

When do I need a counting semaphore?

When you must limit how many threads use a resource, such as a pool of N connections, or count the available items in a producer-consumer buffer.

What is a deadlock?

A situation where threads wait for each other forever, for example thread A holds lock 1 and waits for lock 2 while thread B holds lock 2 and waits for lock 1. Taking locks in a consistent order avoids it.

What is the difference between a spinlock and a mutex?

A spinlock busy-waits and suits very short waits on multicore systems. A mutex puts the waiting thread to sleep, which is better for longer waits.

Check your understanding

Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?

  1. AA mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work
  2. BA mutex keeps using the CPU while waiting, so it suits only short waits
  3. CThey are the same thing under different names
Show answer and explanation

Answer: A. A mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work

When a mutex cannot be acquired, the thread is put into a waiting state and the CPU goes to other threads; the OS wakes it when the lock is released. It wastes no CPU while waiting, so it suits long waits, but sleeping, waking and the context switch cost several microseconds. Real library mutexes combine both strengths, for example by spinning briefly before sleeping.

What is the most suitable use of a counting semaphore?

  1. AAlways doubling the value of a variable
  2. BMerging all program execution into a single thread
  3. CLetting up to four threads at a time use a resource with a fixed number of units, such as four connections
Show answer and explanation

Answer: C. Letting up to four threads at a time use a resource with a fixed number of units, such as four connections

A semaphore holds a counter of remaining units: a thread decrements it before use (waiting if it is 0) and increments it when done. With an initial value of 4, up to four threads can use the resource at once. With 1, it works like a lock for mutual exclusion. Waiting threads are usually put to sleep and woken by the OS.

Which describes a spinlock?

  1. AA lock that puts the thread to sleep until the OS wakes it when the lock is free
  2. BA lock that waits by repeatedly checking the lock variable in a loop until it is free
  3. CA mechanism that protects data by spinning a disk
Show answer and explanation

Answer: B. A lock that waits by repeatedly checking the lock variable in a loop until it is free

A spinlock tries to acquire the lock with an atomic instruction and, if that fails, immediately tries again, over and over. There is no cost of calling the OS to sleep and wake, so it is fast when the lock is held only very briefly. It is common inside OS kernels and for mutual exclusion with interrupt handlers. Its drawback is that it keeps using the CPU while waiting.

  1. Which is an example of deadlock?
  2. What is a characteristic of a reader-writer lock?
  3. When waiting on a condition variable until a queue becomes non-empty, why is the condition rechecked in a while loop rather than an if after waking?

Practice Multicore & Synchronization questions

Key terms explained

Mutex
A lock that only one thread can hold at a time. Typically a thread that cannot get it sleeps and yields the CPU, and the OS wakes it when the lock is released.
Semaphore
A synchronization mechanism holding a counter of remaining units: decrement before use (waiting at 0) and increment when done. Used to manage resources with a fixed number of units.
Spinlock
A lock that waits by repeatedly checking the lock variable in a loop. Fast for short waits, but it keeps using the CPU while waiting.
Deadlock
A state in which threads each hold a resource while waiting for one held by another, so none can proceed. Fixing a global lock order prevents it.

Related comparisons