What is 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.

Field: Multicore & Synchronization

Japanese name: ミューテックス

How a mutex works

A mutex (mutual exclusion lock) is usually a memory word recording whether the lock is held and whether anyone waits. Acquiring it in the uncontended case is a single atomic instruction such as compare-and-swap, done in user space and costing on the order of tens of nanoseconds. Only when the lock is held does the thread ask the operating system to sleep on a wait queue; on Linux this is built on the futex system call. Unlocking checks for waiters and, if there are any, asks the kernel to wake one. A mutex has an owner: only the thread that locked it should unlock it.

Mutex vs. spinlock

A spinlock waiter keeps executing a loop that retries the lock, so it reacts almost immediately to the release but burns its CPU the whole time. A mutex waiter gives up the CPU, which is efficient for long waits but costs a system call, a context switch and a later wake-up, typically several microseconds in total. Spinlocks therefore suit very short critical sections on multicore machines, and places where sleeping is not allowed, such as kernel interrupt handlers. On a single core spinning is pointless, because the holder cannot release the lock while the waiter occupies the CPU. Many library mutexes are adaptive: they spin briefly, then sleep.

Mutex vs. semaphore

A counting semaphore holds an integer and lets up to N threads pass, which makes it a tool for limiting access to a pool of resources or for signaling between threads. Any thread may post a semaphore, so it has no concept of an owner. A mutex is restricted to one holder and is tied to the thread that locked it. That ownership enables error checking and, in real-time systems, priority inheritance, which a plain semaphore cannot offer because the system cannot know which thread to boost.

Pitfalls: priority inversion and deadlock

In a real-time operating system (RTOS), priority inversion occurs when a high-priority task waits for a mutex held by a low-priority task, and a medium-priority task preempts the low one, delaying the high task indefinitely. This caused repeated resets on the Mars Pathfinder lander. Priority inheritance fixes it by temporarily raising the holder to the waiter's priority. The other classic hazard is deadlock: two threads each holding one mutex and waiting for the other's. The standard prevention is to acquire multiple mutexes in a single, fixed global order.

Try it: questions about Mutex

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 add overhead. Some library mutexes combine both strengths, for example by spinning briefly before sleeping.

FAQ

What is the difference between a mutex and a binary semaphore?

Both admit one thread at a time, but a mutex has an owner that must release it, while any thread can signal a semaphore. Ownership is what makes priority inheritance and error checking possible for a mutex.

When should I use a spinlock instead of a mutex?

Use a spinlock only when the critical section is a handful of instructions, the holder is running on another core, and sleeping is impossible or too slow. For anything that might wait longer than a few microseconds, a sleeping mutex is the better choice.

Why is my mutex slow under contention?

Each contended acquisition can involve a system call, a context switch and a wake-up, and the lock's cache line bounces between cores. Shortening the critical section or splitting one lock into several finer-grained locks usually helps more than changing the lock type.

Questions that mention Mutex (3)

Mutex appears in 4 practice questions across 2 topics, including Multicore & Synchronization, Embedded Systems & Firmware.

  1. In which situation is a spinlock least suitable?
  2. Which describes priority inversion in an RTOS?
  3. Which describes priority inheritance, used to prevent priority inversion?

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

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.

Back to the glossary