Mutual exclusion (often abbreviated to mutex)
algorithms are used in
concurrent programming to avoid the simultaneous use of a common resource, such as a
global variable, by pieces of computer code called
critical sections. Examples of such resources are fine-grained
flags, counters or
queues, used to communicate between code that runs concurrently, such as an application and its
interrupt handlers. The problem is acute because a
thread can be stopped or started at any time.To illustrate: suppose a section of code is mutating a piece of data over several program steps, when another thread, perhaps triggered by some unpredictable event, starts executing. If this second thread reads from the same piece of data, the data, in the process of being overwritten, is in an inconsistent and unpredictable state. If the second thread tries overwriting that data, the ensuing state will probably be unrecoverable. These
critical sections of code accessing shared data must therefore be protected, so that other processes which read from or write to the chunk of data are excluded from running.
See more at Wikipedia.org...
<
parallel> A
mutual exclusion object that allows multiple
threads to synchronise access to a shared resource. A mutex has two states: locked and unlocked. Once a mutex has been locked by a thread, other threads attempting to lock it will block. When the locking thread unlocks (releases) the mutex, one of the blocked threads will acquire (lock) it and proceed.
If multiple threads or tasks are blocked on a locked mutex object, the one to take it and proceed when it becomes available is determined by some type of scheduling algorithm. For example, in a priority based system, the highest priority blocked task will acquire the mutex and proceed. Another common set-up is put blocked tasks on a first-in-first-out queue.
See also:
priority inversion(2002-03-14)