When one thread using a resource prevents another thread from safely using it, that resource is mutually exclusive. This is typically implemented as a binary semaphore.

To use a mutex a thread must:

  1. Call semaphore wait
  2. Do the thing
  3. Release the semaphore

Only the thread that acquired the mutex can release it. This enforces mutual exclusions.

Warning1

These are traffic lights not Barriers. These are agreed upon signals but do not prevent actual access.

Rust

In Rust, Mutex’s wrap a particular type. Multiple threads need access to this mutex but the mutex type needs to outlive other threads but we can’t move it into more than one thread due to Rust’s ownership rules. This means we need to break some rules inadvertently with Multiple Ownership.