Code on a screen, representing a mutex lock.

A lock that lets only one task use a shared resource at a time.

What a mutex is

A mutex, short for 'mutual exclusion', is a mechanism used in programming to make sure that only one task can access a particular shared resource at a time. When several tasks run concurrently and need to use the same data or resource, a mutex acts like a lock on a door: whoever holds the lock gets exclusive use, and everyone else must wait until it is released. This simple idea is one of the most important tools for writing correct concurrent programs.

The problem it solves

When multiple tasks run at the same time and share data, they can interfere with one another. If two tasks try to update the same value simultaneously, the result can be wrong or corrupted - a classic concurrency bug. A mutex prevents this by ensuring that only one task at a time can enter the critical section of code that touches the shared resource, so updates happen cleanly one after another rather than clashing.

How a mutex works

A mutex has two states: locked and unlocked. Before a task uses the shared resource, it tries to 'acquire' or lock the mutex. If the mutex is free, the task locks it and proceeds; if another task already holds it, the requesting task waits until the mutex becomes available. When the task finishes, it 'releases' or unlocks the mutex, letting a waiting task take its turn. This acquire-use-release cycle enforces one-at-a-time access.

Mutexes and critical sections

The piece of code that accesses a shared resource and must not be run by more than one task at once is called a critical section. A mutex guards the critical section: a task must hold the mutex to be inside it. By wrapping critical sections with a mutex, programmers ensure these sensitive operations do not overlap, which keeps shared data consistent even when many tasks are running concurrently and competing for it.

Using mutexes carefully

While mutexes are powerful, they must be used carefully. A task that locks a mutex and forgets to release it can leave others waiting forever. Worse, if two tasks each hold one mutex and wait for the other's, they can deadlock, freezing both. Good practice is to hold a mutex for as short a time as possible and to be consistent about the order in which multiple mutexes are acquired, reducing the risk of these problems.

Why it matters

Mutexes are a cornerstone of concurrent programming, making it possible for multiple tasks to safely share data without corrupting it. Understanding what a mutex is - a lock that grants one task at a time exclusive access to a shared resource - clarifies how software avoids a whole class of timing bugs. For anyone writing programs that do several things at once, the mutex is an essential concept to understand.

See also: What is a race condition? Explained simply, What is a deadlock? Explained simply.

Sources

Published date reflects the original event date (2023-08-15). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.

Skillo Staff

Skillo Staff

All stories by: Skillo Staff

Leave a Reply