$linuxjunkies
>

futex

also: fast userspace mutex

A futex (fast userspace mutex) is a kernel-assisted synchronization primitive that allows processes or threads to efficiently coordinate access to shared resources with minimal system calls.

A futex combines userspace spin-locking with kernel arbitration to provide fast mutual exclusion. In the uncontended case (when no other thread is waiting), a futex requires only a few CPU instructions in userspace. When contention occurs, the kernel is asked to put threads to sleep and wake them efficiently, avoiding busy-waiting.

Futexes are the foundation of modern Linux thread synchronization. Higher-level constructs like pthread_mutex_t, condition variables, and semaphores are typically implemented using futexes under the hood.

Example: When two threads try to acquire the same mutex, the first succeeds with just a fast userspace atomic operation. The second thread efficiently blocks in the kernel without consuming CPU cycles, and is awakened when the first thread releases the lock.

Related terms