Deadlocks

livelock

Remember the two polite people in the narrow corridor who keep stepping aside for each other, both dodging the same way, then both dodging back, over and over? They are extremely BUSY — constantly moving — yet they never get past each other. That is livelock: processes that keep changing state and doing work, but make no actual progress, endlessly reacting to one another instead of advancing.

Livelock is the active cousin of deadlock. In a true deadlock the stuck processes are BLOCKED — they sit waiting and use no CPU. In a livelock the processes are RUNNABLE and busy: each repeatedly takes some action (back off, retry, yield, try again) in response to the others, but the combination of their actions never lets any of them complete. A common cause is a naive deadlock-avoidance attempt: 'if I can't get the second lock, release the first and retry.' If two threads do this in lockstep, they release and re-grab forever in perfect, unproductive synchrony. The cure is usually to add randomness (random backoff, like Ethernet's collision handling) or asymmetry so the threads stop mirroring each other.

The honest distinction matters because livelock can fool you. Tools that detect deadlock by looking for blocked, waiting processes will see nothing wrong — the CPU is busy, threads are running, the program 'looks alive.' Yet no useful work is happening, which can be harder to diagnose than a frank deadlock. Livelock is also related to but different from starvation: in starvation a particular process is perpetually denied while others make progress; in livelock the whole interacting group spins without progress.

Two threads each try to grab both locks with this rule: lock(first); if try_lock(second) fails, release(first) and retry. In perfect step they both grab their first lock, both fail the second, both release, and loop forever — busy but stuck. Adding a small random delay before retry breaks the symmetry.

Livelock: constant activity, zero progress, because the threads keep mirroring each other.

Livelock differs from deadlock: deadlocked processes are blocked and use no CPU, whereas livelocked processes are busy and runnable yet make no progress — so blocked-process deadlock detectors miss it entirely.

Also called
live-lock活鎖