Mutual Exclusion

Mutual Exclusion Lock (Mutex)

Also see mutex (C++).

Mutex = Mutual Exclusion Lock

A mutex is a programming flag used to grab and release an object. When data are acquired that cannot be shared, the mutex is set to lock (typically zero), which blocks other attempts to use it.

The mutex is set to unlock when the data are no longer needed or the routine is finished.

Mutex vs. Binary Semaphore?

For a mutex, the process that locks the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1). In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it.

  • In some textbooks, there is no distinction

Below is an example that uses Semaphore to implement Mutex

/* program mutualexclusion */
const int n = /* number of processes */;
semaphore s = 1;
 
void P(int i) {
  while (true) {
    semWait(s);
    /* critical section */;
    semSignal(s);
    /* remainder */;
  }
}
 
void main() {
  parbegin(P(1), P(2), ... , P(n));
}