ArticleslgStudy

science

Futex

Futex is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Futex rather than just read about it. In short: In computing, a futex (short for "fast userspace mutex") is a kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables. A futex consists of a kernel-space wait queue that is attached to an atomic integer in userspace.

Key takeaways

  • Futex belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Futex to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Futex from memory before moving on to harder problems.

Reference excerpt

In computing, a futex (short for "fast userspace mutex") is a kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables. A futex consists of a kernel-space wait queue that is attached to an atomic integer in userspace. Multiple processes or threads operate on the integer entirely in userspace (using atomic operations to avoid interfering with one another), and only resort to the fast but still more expensive system calls to request operations on the wait queue (for example to wake up waiting processes, or to put the current process on the wait queue). A properly programmed futex-based lock will not use system calls except when the lock has contention; since most operations do not require arbitration between processes, this will not happen in most cases.

History Hubertus Franke (IBM Thomas J. Watson Research Center), Matthew Kirkwood, Ingo Molnár (Red Hat), and Rusty Russell (IBM Linux Technology Center) originated the futex mechanism on Linux in 2002. In the same year, discussions took place on a proposal to make futexes accessible via the file system by creating a special node in /dev or /proc. However, Linus Torvalds strongly opposed this idea and rejected any related patches. Futexes then appeared for the first time in version 2.5.7 of the Linux kernel development series; the semantics stabilized as of version 2.5.40, and futexes have been part of the Linux kernel mainline since the December 2003 release of 2.6.x stable kernel series. Futex functionality has been implemented in Microsoft Windows since Windows 8 or Windows Server 2012 under the name WaitOnAddress. In 2013, Microsoft patented futex-related WaitOnAddress and the patent was granted in 2014. In May 2014, the CVE system announced a vulnerability discovered in the Linux kernel's futex subsystem that allowed denial-of-service attacks or local privilege escalation. In May 2015, the Linux kernel introduced a deadlock bug via Commit b0c29f79ecea that caused a hang in user applications. The bug affected many enterprise Linux distributions, including 3.x and 4.x kernels, and Red Hat Enterprise Linux version 5, 6 and 7, SUSE Linux 12 and Amazon Linux. Futexes have been implemented in OpenBSD since 2016. The futex mechanism is one of the core concepts of the Zircon kernel in Google's Fuchsia operating system since at least April 2018. Apple implemented futex in iOS/iPadOS/tvOS 17.4, macOS 14.4, watchOS 10.4 and visionOS 1.1. Futex like functionality was added to C++ with the atomic::wait, atomic::notify_one, and atomic::notify_all operations in C++20. Support for FUTEX2 in the Linux kernel was designed to support two new main features, first something that can be used to implement the Win32 API WaitForMultipleObjects, and second to be able to wait on addresses other than 32bit ones. The first step was integrated in 5.16 in November 2021. with the waitv syscall.

Operations Futexes have two basic operations, WAIT and WAKE.

WAIT(addr, val) If the value stored at the address addr is val, puts the current thread to sleep. WAKE(addr, num) Wakes up num number of threads waiting on the address addr. For more advanced uses, there are a number of other operations, the most used being CMP_REQUEUE and WAKE_OP, which both function as more generic WAKE operations.

CMP_REQUEUE(old_addr, new_addr, num_wake, num_move, val) If the value stored at the address old_addr is val, wakes num_wake threads waiting on the address old_addr, and enqueues num_move threads waiting on the address old_addr to now wait on the address new_addr. This can be used to avoid the thundering herd problem on wake. WAKE_OP(addr1, addr2, num1, num2, op, op_arg, cmp, cmp_arg) Will read addr2, perform op with op_arg on it, and store the result back to addr2. Then it will wake num1 threads waiting on addr1, and, if the previously read value from addr2 matches cmp_arg using comparison cmp, will wake num2 threads waiting on addr2. This very flexible and generic wake mechanism is useful for implementing many synchronization primitives. FUTEX2 API:

WAITV(waiters: {addr, val, flags}*, num) Wait on many, wake on any. For each of the num entries in the waiters vector do a parallel WAIT operation on the address and value. Return the index of the first address being awoken. The flags can be used to indicate different bit widths.

See also Synchronization Fetch-and-add Compare-and-swap

References

External links futex(2) - futex() system call futex(7) - futex semantics and usage Hubertus Franke, Rusty Russell, Matthew Kirkwood. Fuss, futexes and furwocks: Fast Userlevel Locking in Linux, Ottawa Linux Symposium 2002. Drepper, Ulrich (2011). "Futexes are Tricky" (PDF). 1.6. Red Hat. Bert Hubert (2004). Unofficial Futex manpages Ingo Molnar. "Robust Futexes", Linux Kernel Documentation "Priority Inheritance Futexes", Linux Kernel Documentation

Worked examples

Example 1 — a first encounter with Futex

Start with the simplest possible case. Write down what Futex claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Futex before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Futex ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Futex

In research
Futex appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Futex in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Futex is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrency control, Linux kernel features, so understanding it makes those chapters shorter.
In everyday life
Look for Futex outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Futex” →

How to study Futex in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Futex means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Futex out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Futex in simple terms?

In computing, a futex (short for "fast userspace mutex") is a kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables. A futex consists of a kernel-space wait que…

Why does Futex matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Futex?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Futex.

Tags

  • Concurrency control
  • Linux kernel features

Keep exploring