A locking primitive, a mutex is an object of some type used to prevent multiple concurrent threads of execution from munging data incorrectly or following an algorithm incorrectly.

The term "mutex" is short for "mutual exclusion", literally the prevention of more than one code path from doing something at the same time. It has two states: locked or unlocked. Information on the owner may be stored, allowing for easier debugging as well as the possibility of recursion. Only the owner must unlock a mutex which it has locked.

A recursive mutex must also have a reference count, and may be repeatedly locked by its current owner and then repeatedly unlocked the same number of times. This is sometimes useful, but not usually for new code.

Compare: atomic access, semaphore.