operating-systems concurrency

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:

OperationSemanticsEffect
lock()AcquireIf unlocked, mark as locked and proceed; otherwise block until available
unlock()ReleaseMark 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 execute unlock(). 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.