Mutex vs Semaphore: What Is the Difference?

Blog / Mutex vs Semaphore: What Is the Difference?

Mutex vs semaphore: what is the difference between a mutex and a semaphore? And why do software engineering interviewers love asking about it?

Two Threads, One Counter

Imagine two threads both adding 1 to a shared counter.

Each thread reads the value, adds 1, and writes it back. One thread's update can easily overwrite the other, and the final count comes out wrong.

Two threads both read 0, both compute 0 + 1 = 1 and both write 1, so the counter ends at 1 instead of 2

This exact problem is called a race condition.

The code that touches the shared data is the critical section. Only one thread should run it at a time.

Python's GIL does not prevent this, because counter += 1 is several steps.

How a Mutex Works

A mutex (mutual exclusion) is a lock.

A thread locks it before the critical section and unlocks it after. Any other thread that tries to lock it waits until it is unlocked.

A mutex has an owner. Only the thread that locked it can unlock it.

Because the system knows the owner, it can use priority inheritance to stop a high priority thread waiting forever on a low priority one.

But what if we want to allow more than one thread to access a resource? This is where a semaphore comes in.

How a Semaphore Works

A semaphore is a counter of permits.

The acquire operation takes a permit, or waits if the count is 0. The release operation gives one back.

A semaphore created with 3 permits lets up to 3 threads in at once. This is useful for limiting 3 connections to a database.

A semaphore with 3 permits lets Threads 2, 3 and 4 into the pool and on to the database, while Thread 1 has released its permit

A semaphore has no owner, so any thread can release it.

This is why it is also used for signalling between threads, where one thread waits until another signals that work is ready.

Binary Semaphore vs Mutex

This brings us to the classic interview trap.

A binary semaphore only counts 0 or 1, so it looks like a mutex. But it has no owner.

Another thread can release it, and nothing stops a thread from releasing a permit it never took. It also lacks priority inheritance.

A binary semaphore has no owner, lets another thread release it and has no priority inheritance. A mutex has an owner, only the owner releases it, and it has priority inheritance

A Lock Next to a Semaphore in Python

Let us look at how this works in Python.

python
import threading counter = 0 lock = threading.Lock() with lock: counter += 1

Here we create a threading.Lock to protect our shared counter.

The with statement acquires the lock before the addition. It always releases the lock automatically after the math finishes.

python
import threading db_semaphore = threading.Semaphore(3) with db_semaphore: print("Using 1 of 3 database connections")

Next we create a threading.Semaphore(3) initialized with 3 permits.

The with statement acquires one of those permits before using the database connection. It then releases the permit back to the pool once the work is done.

Common Mistakes

Deadlock

This happens when two threads each hold one lock and wait for the other's lock, so neither can continue.

Always taking locks in the same order prevents it.

Forgetting to release

Forgetting to release a lock, for example when an exception is thrown, blocks every other thread.

This is why you should always use the with statement.

Releasing too often

Python provides a BoundedSemaphore, which raises a ValueError if it is released more times than it was acquired.

Recap

Feature

Mutex

Semaphore

What it is

Exclusive lock

Counter of permits

Owner

The locking thread

No owner

How many threads at once

Exactly one

Up to the limit

Who releases it

The acquiring thread

Any thread

Typical use

Protect shared data

Limit access or signal

Use a mutex to protect shared data, and use a semaphore to limit access or signal events.

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.