$linuxjunkies
>

mutex

also: mutual exclusion lock, lock

A mutex (mutual exclusion lock) is a synchronization primitive that ensures only one thread can access a shared resource at a time, preventing race conditions and data corruption.

A mutex is a locking mechanism used in multithreaded programs to protect critical sections of code where shared data is accessed or modified. When a thread acquires a mutex, other threads attempting to acquire it must wait until the lock is released.

In Linux, mutexes are implemented at both the kernel level (via futex syscalls) and in userspace libraries like pthreads. A typical pattern is: pthread_mutex_lock() before accessing shared data, then pthread_mutex_unlock() after. If thread A holds the mutex, thread B will block until A releases it.

Example: two threads incrementing a shared counter without a mutex causes race conditions where increments are lost. With a mutex protecting the counter, all updates are serialized and correct.

Related terms