liveBook · Manning

Futex_wait

As explained in Learn Concurrent Programming with Go

futex_wait is a futex operation that suspends an execution when a memory address still contains an expected value.

futex_wait suspends an execution only when a specified memory address still contains an expected value. It acts as a wait-queue primitive rather than a mutex itself. section 12.3.1

Calling futex_wait(addr, value) produces one of two outcomes:

  • Matching value: suspends the caller and places it at the back of the queue associated with that address. section 12.3.1
  • Different value: returns immediately, allowing execution to continue without sleeping. section 12.3.1

A lock can combine an atomic compare-and-swap with futex_wait: failed acquisition waits only while the lock still has the expected locked state. Unlocking stores zero and wakes a waiter. section 12.3.1

A contention-aware version marks the lock with waiters, using value 2, and retries after waking. This can avoid unnecessary system calls when no execution is waiting. section 12.3.2

Figure 12.6 Calling futex_wait() with two different outcomes

Compare the matching-value suspension path with the nonmatching-value immediate-return path. section 12.3.1

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains address/value matching, queueing, wake counts, and contention-aware locking while leaving operating-system parameters abstract.
Examples
Uses pseudo-Go FutexLock implementations, address-based wait outcomes, waiter state 2, and conditional wake-ups.
What it assumes
Assumes atomic CompareAndSwap, Swap, and Store operations as the lock’s state-management mechanisms.
What you can do after
Build a user-space lock that sleeps on contention and wakes waiting executions after release.
How it connects
Connects futex_wait with atomic operations, futex_wake, mutexes, semaphores, condition variables, and system calls.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.