Definition
Mutex
A mutex (mutual exclusion lock) is a synchronization primitive that ensures only one process or thread can access a critical section at a time. It provides exclusive ownership: the thread that acquires the mutex must be the one to release it.
Operations
A mutex supports two fundamental operations:
| Operation | Semantics | Effect |
|---|---|---|
lock() | Acquire | If unlocked, mark as locked and proceed; otherwise block until available |
unlock() | Release | Mark as unlocked; if threads are waiting, wake one to acquire |
Mutual Exclusion Protocol
Mutex m // initially unlocked
Void process()
while (true) {
m.lock(); // acquire exclusive access
/* --- Critical Section --- */
m.unlock(); // release exclusive access
/* --- Remainder Section --- */
}
Properties
Ownership
Unlike a binary semaphore, a mutex enforces ownership: only the thread that successfully executed
lock()may executeunlock(). Attempting to unlock a mutex held by another thread results in undefined behaviour or an error.
Priority Inheritance
Some mutex implementations support priority inheritance to prevent priority inversion. When a high-priority thread waits on a mutex held by a low-priority thread, the holder temporarily inherits the higher priority to avoid indefinite blocking.