AlgoViz

Threads, locks and lost progress

easy 8 min

Threads in a process share memory and descriptors.

Video lesson · Chapter 1 of 8

A slow dependency inside a critical section

0:00 / 5:12

Theory

Read or listen first. About 3 min.

What it is

Threads in a process share memory and descriptors. Synchronization establishes who may change shared state and which earlier writes become visible. A critical section serializes callers for its whole duration, including any time the owner sleeps. Spinning, blocking and lock-free retries move the cost to different resources.

Why it exists

A profile shows idle CPUs during a throughput collapse. A lock-wait trace shows many callers behind one owner that is waiting for a remote write. Compare hold time with wait time before changing the primitive. Moving work out of a lock changes the atomic boundary, so reserve or snapshot the state under the lock and validate it when the write finishes.

If hold times stay short while queues grow, measure arrival rate and the number of acquisitions per request.

A slow dependency inside a critical section

Request threads → Shared mutex (wait to acquire). Lock owner → Shared mutex (hold mutex). Lock owner → Remote service (write while holding)
Request threads → Shared mutex (wait to acquire). Lock owner → Shared mutex (hold mutex). Lock owner → Remote service (write while holding)

Inside this chapter

Kernel Threads vs. User Threads (Green Threads)
A runtime can multiplex user threads or tasks over kernel threads. One blocking operation’s effect depends on that runtime and whether it can keep other tasks scheduled.
POSIX Threads (pthreads)
Pthreads share process memory but have separate stacks and scheduling state. Protect shared invariants and decide who joins or detaches each thread on exit.
Race Conditions and Critical Sections
A critical section protects an invariant across several operations. Two individually valid reads followed by writes can still lose an update when their sequences interleave.
Deadlocks: Detection, Prevention, and Recovery
Deadlock can form when owners wait in a cycle for resources they exclusively hold. A consistent acquisition order prevents such cycles for the locks covered by that order.
Livelocks and Busy Waiting
In livelock participants keep changing state yet finish no work. Busy waiting can consume CPU during either useful contention or a protocol that never makes progress.
Starvation and Priority Inversion
A low-priority lock owner can block a higher-priority waiter while medium-priority work delays release. Priority inheritance can reduce this specific form of inversion.
Mutexes: Mechanics and Best Practices
A mutex grants exclusive ownership. Measure hold duration and acquisition wait separately. Sleeping on a network call while holding the mutex extends everybody’s wait.
Semaphores: Counting and Binary
A counting semaphore represents available permits. A binary semaphore has one permit, but need not have a mutex’s owner-only release semantics.
Spinlocks: When and Why to Use Them
Spinning avoids a sleep/wakeup round trip only when the expected hold is short and the owner can run. Oversubscription can make spinning particularly costly.
Atomic Operations and Compare-and-Swap (CAS)
Compare-and-swap conditionally replaces a value if it still equals an expected value. A failed comparison normally requires rereading state and recomputing the proposed change.
Memory Barriers and Ordering
Ordering rules govern when other threads may observe writes. A release operation paired with a suitable acquire operation can publish initialized data across a handoff.
Futex internals (Fast Userspace Mutex)
A futex lets a thread sleep only if a userspace word still has the expected value. Uncontended locking can therefore stay entirely in userspace.
Read-Write Locks (rwlock)
A read-write lock allows multiple readers or an exclusive writer. Heavy reads can delay writers depending on policy. Count writer wait as well as aggregate throughput.
Condition Variables
A condition variable wakes threads waiting for a predicate. Recheck the predicate while holding its associated mutex because another thread may have consumed the work.
Thread-Local Storage (TLS)
Thread-local storage gives each thread its own instance of a value. A pooled thread may serve many requests, so request-specific data still needs explicit cleanup.

How it works

Step through the idea, or press Play.

Step 1 of 4

Acquire once. A mutex fast path can claim an uncontended lock with an atomic operation.

Understand it

Worked incident

A slow dependency inside a critical section

ObservationLock wait p95 (ms)Interpretation
Baseline2The lock protects a short in-memory update.
Incident180One remote write holds the lock while other requests queue.
After intervention5Move the call outside the critical section with a version check. Verify invariants under concurrency.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If hold times stay short while queues grow, measure arrival rate and the number of acquisitions per request.

Summary

The whole topic in one minute.

A blocked owner cannot release the lock until the dependency responds. Busy waiting would spend CPU without shortening that call.

If hold times stay short while queues grow, measure arrival rate and the number of acquisitions per request.

Sources