Mutex

From Rosetta Code
Revision as of 09:13, 27 August 2008 by rosettacode>Dmitry-kazakov (Created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Mutex (abbreviated Mutually Exclusive access) is a synchronization object, a variant of semaphore with k=1. Mutex is said to be seized by a task decreasing k. It is released when the task restores k. Mutexes are typically used to protect a shared resource from concurrent access. A task seizes the mutex, then accesses the resource, and after that releases the mutex.

Mutex is a low-level synchronization primitive exposed to deadlocking. A deadlock can be constructed already with two tasks and two mutexes. Entering the deadlock is usually aggravated by a race condition state.

Variants of mutexes

Global and local mutexes

Usually OS provides various implementations of mutexes corresponding to the variants of tasks available in the OS. For example, system-wide mutexes can be used by processes. Local mutexes can be used only by threads etc. This distinction is maintained because, depending on the hardware, seizing a global mutex might be thousand times slower than seizing a local one.

Reentrant mutex

A reentrant mutex can be seized by the same task multiple times. Each seizing of the mutex is matched by releasing it, in order to allow other task to seize it.

Read write mutex

A read write mutex can be seized at two levels for read and for write. The mutex can be seized for read by any number of tasks. Only one task may seize it 'write. Read write mutex are usually used to protect resources which can be accessed in a mutable and immutable ways. Immutable (read) access is granted concurrently for many tasks because they do not change the resource state. Read write mutexes can be reentrant, global or local. Further, promotion operations may be provided, that's when a task that have seized the mutex for write releases it keeping seized for read. Note that the reverse operation is potentially deadlocking and requires some additional access policy control.

Deadlock prevention

There exists a simple technique of deadlock prevention when mutexes are seized in some fixed order.