Level 1: Bronze
A memorized definition. Not wrong, but it stops there.
Both synchronize threads. A mutex lets one in, a semaphore lets several in.
3 stars If you are short on time, start with this one.
In one or two sentences
A mutex lets one thread at a time into a critical section. A semaphore is a counter that lets in a set number at a time.
Say it out loud, or type it below. What you type never leaves this device.
A memorized definition. Not wrong, but it stops there.
Both synchronize threads. A mutex lets one in, a semaphore lets several in.
Correct, but too broad or missing context.
A mutex is a lock for mutual exclusion that the locking thread releases. A semaphore is a counter. At zero you wait, and you wake when another thread raises it.
Reaches principle, use and trade-off, with words that invite the next question.
A mutex has an owner, and normally the thread that locked it unlocks it, so it guards a critical section. A semaphore has no owner, so any thread can signal it. That suits limiting a number of resources or ordering work as in producer and consumer. A binary semaphore looks like a mutex, but with no owner it cannot catch a missed release or a release from the wrong place. Use a mutex for mutual exclusion and a semaphore for counts and signals.
These are likely next questions from an interviewer. Think of your answer first, then press the button.
1 / 3
What is a critical section?
A stretch of code that touches shared state and gives wrong results if several threads run it at once. Mutual exclusion is the guarantee that only one is inside.
2 / 3
How is a spinlock different from a mutex?
A spinlock burns CPU and keeps checking until the lock frees. A mutex puts the waiter to sleep and gives up the CPU. For very short waits a spinlock saves the switch cost. For long waits a mutex is better.
3 / 3
How do you solve producer and consumer with semaphores?
Use two semaphores that count empty and full slots, and one mutex that guards the buffer. The producer waits for an empty slot, fills it and raises the full count. The consumer does the reverse.