Threads, locks and lost progress A slow dependency inside a critical section A slow remote write has stopped a whole room of workers, although only one worker made that call. Niko follows the waiting threads back to the owner of a shared lock. The example's ninety-fifth percentile lock wait rises from two milliseconds to a hundred eighty. The owner is waiting on a remote dependency while holding the lock. More ready callers create a longer queue behind that one owner. Threads in a process share memory and descriptors. Synchronization establishes who may change shared state and which earlier writes become visible. Request threads in context 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. Pthreads share process memory but have separate stacks and scheduling state. Protect shared invariants and decide who joins or detaches each thread on exit. 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. 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. One checkout thread takes the inventory mutex, then waits on a remote price lookup while still holding it. Nine other threads need only a brief local update, but all queue behind the same owner. A CPU profile can show little activity during the wait because the expensive interval consists of waiting for permission to continue. Follow the owner through the wait A mutex fast path can claim an uncontended lock with an atomic operation. A contender checks the lock word and may enter a kernel-assisted sleep. The owner releases the mutex and the implementation wakes eligible waiters. A spinlock would spend CPU until release. That is particularly wasteful while the owner waits for remote I O. Suppose a thread spins for a lock while the owner was descheduled on the only available core. Spinning consumes precisely the resource the owner needs to release it. Blocking allows other runnable work to proceed, but has a wakeup cost for very short holds. Compare the distribution of hold times and effective processor availability before choosing a synchronization primitive based on its uncontended benchmark. Read the measurements Moving the remote call outside the lock can shorten the queue. Think through the state you read before releasing it. Another thread can change inventory during the call. Use a version check or revalidate the decision under the lock before committing. A performance fix that produces negative inventory has failed the requirement. A ninety-fifth percentile lock wait is computed over acquisitions that were observed. If cancelled requests abandon the queue before acquiring, their missing waits can make the successful population look better during a worse incident. Count abandoned and successful attempts together, with their admission and deadline times. Move the call without breaking the invariant Can Niko just unlock before the remote write and write its result back afterward? Another worker could change the protected object in between. Capture a version or reserve the intended state under the lock, perform the slow operation, then validate the reservation before installing the result. If hold times stay short while queues grow, measure arrival rate and the number of acquisitions per request. Check the competing explanation 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. 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 gives each thread its own instance of a value. A pooled thread may serve many requests, so request-specific data still needs explicit cleanup. Prove both speed and correctness The revised path has a five-millisecond ninety-fifth percentile wait under the matched workload. 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. Niko also runs concurrent changes against the same object. A shorter queue is a win only if the resulting state still obeys the original rule. Apply the diagnosis Now the lock owner itself is runnable but receives little processor time. Increasing the number of workers adds waiters and may make scheduling worse. Trace acquisition, ownership and release for one lock instance. Check owner CPU time alongside elapsed hold time before deciding to split the lock or change the scheduler.