Home

futex

Futex, short for fast userspace mutex, is a synchronization primitive used in the Linux kernel and user space to implement locks with a fast path in user space and a kernel fallback when contention occurs. It is designed to minimize kernel involvement for uncontended operations while providing a mechanism for blocking and waking threads when contention happens.

The common pattern is to acquire a lock using atomic operations in user space. If the lock

The futex system call accepts a pointer to a user-space address, an operation code (such as wait

In practice, futexes underlie many user-space locking primitives, notably the pthreads mutex and condition variables on

Limitations include the need for correct user-space synchronization and the possibility of spurious wakeups; robust usage

is
uncontended,
the
atomic
operation
succeeds
and
the
thread
proceeds.
If
contention
occurs,
a
thread
may
call
the
futex
system
call
to
wait
on
a
specific
address;
the
kernel
blocks
the
thread
until
another
thread
wakes
it.
or
wake),
and
a
value
used
by
the
operation;
additional
arguments
permit
timeouts
and
cross-address
requeueing.
A
FUTEX_PRIVATE_FLAG
may
be
used
to
indicate
the
futex
is
private
to
the
process,
avoiding
cross-process
wakeups.
Linux.
The
design
minimizes
kernel
involvement
for
uncontended
locks
and
enables
efficient
scaling
on
multi-core
systems;
advanced
variants
support
wake-up
on
specific
conditions
and
requeueing
waiters.
relies
on
rechecking
conditions
after
wake.
Futexes
are
specific
to
Linux's
API
and
are
not
portable
across
all
operating
systems.