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.
Aspect
Mutex
Semaphore
Purpose
Mutual exclusion: one thread at a time in a critical section
Counting: limit access to a pool of N resources, or signal events between threads
Value
Locked or unlocked (binary)
A non-negative integer count (a binary semaphore is the special case of 0 or 1)
Ownership
Owned by the thread that locked it, and that thread should unlock it
No owner: any thread can post or signal
Operations
Lock and unlock
Wait (P, decrement) and post (V, increment)
Signaling between threads
Not intended for it
Yes: for example a producer signaling a consumer
Priority inversion
Often avoided with priority inheritance
Usually not supported
Typical uses
Protecting a shared data structure such as a list or a counter
Limiting a connection pool, bounded producer-consumer buffers, signaling from an interrupt handler to a task
Common pitfalls
Deadlock from inconsistent lock order, or forgetting to unlock
Lost 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.
AA mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work
BA mutex keeps using the CPU while waiting, so it suits only short waits
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 add overhead. Some library mutexes combine both strengths, for example by spinning briefly before sleeping.
BMerging all program execution into a single thread
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.
AA lock that puts the thread to sleep until the OS wakes it when the lock is free
BA lock that waits by repeatedly checking the lock variable in a loop until it is free
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.
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.
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.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.