ContentsThe library

What Two Threads Can Break

Four Things Must All Be True, So Breaking Any One of Them Is Enough

Last timeOnly One at a Time

Deadlock needs four conditions at once. That is good news, because it means there are four places to attack it, and in practice one of them wins almost every time.

The four conditions

The previous lesson ended with a transfer that needs to hold two locks at once.

That is where deadlock enters, and the subject has an unusually clean structure:

four conditions must all hold at the same time, and removing any one of them

makes deadlock impossible.

The first is exclusive holding. A resource is held by one thread at a time. If

anything can be shared freely, nobody waits for it.

The second is holding while waiting. A thread keeps what it has while asking

for more. A thread that acquired everything in one go, or nothing, would never

be stuck mid-collection.

The third is that nothing is taken away by force. A thread keeps a lock until

it chooses to release it. If the system could remove a lock from a thread, it

could always break a jam.

The fourth is a cycle of waiting. Thread one waits for something thread two

holds, and following that chain brings you back to thread one.

FIG 1The same two locks in two orders
plaintext
thread one, transferring from A to B:

  take lock A
  take lock B
    move the money
  release lock B
  release lock A

thread two, transferring from B to A:

  take lock B
  take lock A
    move the money
  release lock A
  release lock B
Two transfers in opposite directions. Each function is correct. Each takes both locks it needs and releases both. Run them at the same moment and neither will ever finish.
FIG 2The interleaving that stops everything
stepstepthread onethread twostatewhat happened
11takes lock Arunsboth threads progressingNothing unusual. Thread one holds one of the two locks it needs.
22runstakes lock Bone lock eachStill nothing wrong. Each thread holds exactly one lock and intends to take the other.
33asks for lock B, waitsrunsthread one is blockedThread one is waiting for thread two to release B. That is ordinary and would resolve in a moment under any other order.
44waitsasks for lock A, waitsnobody will ever moveThread two now waits for thread one. Each holds what the other needs and neither will release until it gets what it is waiting for.
4 steps
Four steps, and only the last one is fatal. Up to step three everything is normal operation. The program does not crash, log anything or use any processor time: the two threads simply stop, which is why deadlock is often reported as the service hanging rather than as an error.

The cycle

Drawing the waiting makes the fourth condition concrete and gives a diagnostic

that works on real incidents.

FIG 3Who is waiting for whom
The loop is the whole diagnosis. Any thread dump from a stuck service can be turned into this picture by listing what each blocked thread holds and what it is waiting for, and the cycle is usually visible within a minute of drawing it.

This is the practical skill. When a service hangs, take a thread dump, write

down each blocked thread with what it holds and what it wants, and look for a

loop. If there is one, you have both the diagnosis and the fix, because the fix

is always to break the loop at the cheapest point.

The lesson stops here

4 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Two Threads, Four Steps, and Six Different Programs You Did Not Write
  2. 02The Statement You Wrote Is Three Instructions and the Danger Lives Between Themopening only
  3. 03A Perfectly Correct Lock Around Exactly the Wrong Piece of Codeopening only
  4. 04Four Things Must All Be True, So Breaking Any One of Them Is Enoughyou are here
  5. 05The Loop Is Still Spinning and the Flag Was Set Ten Minutes Agoopening only
  6. 06One Relation Settles Every Argument About What a Thread Is Allowed to Seeopening only
  7. 07Write It Only If Nobody Moved It, and Try Again If They Didopening only
  8. 08Seven Lessons of Difficulty That All Disappear If Nobody Shares Anythingopening only

Read alongside