Drawing the graph
A resource allocation graph is a picture of who holds what and who waits for what. Threads are circles. Resources, here two locks, are boxes, with one dot for each copy. Locks A and B have one copy each.
Line 1: T1 locks A. A is free, so T1 gets it at once. We draw an assignment arrow from A to T1. It points at the holder. T1 keeps running.
Line 2: T2 locks B, which is also free. Another assignment arrow, from B to T2. So far nobody waits: each thread holds one lock and keeps running.
Line 3: T1 asks for B, but T2 holds it, so T1 must wait (it blocks). We draw a request arrow from T1 to B, dashed here. A request arrow means: this thread is stuck until that resource is free.
Line 4: T2 asks for A, which T1 holds. T2 waits too, with a request arrow from T2 to A. Now both threads are blocked, and each one waits for the lock the other one holds.
Reading a cycle
Start at T1 and follow the arrows: T1 to B, B to T2, T2 to A, and A back to T1. That closed loop is a cycle. When every resource has one copy, a cycle means deadlock: these threads wait forever.
More than one copy
A new story: B has two copies, and a third thread, T3, holds the second one. Every arrow from before is still there, so the same cycle is still there. Is this still a deadlock?
T3 has no request arrow: it waits for nothing. So it can finish its work and unlock its copy of B. Its arrow disappears, and B now has a free copy.
The free copy goes to T1, so its request arrow turns into an assignment arrow. T1 finishes and unlocks A and B, then T2 gets A. Here the cycle didn't mean deadlock.
No cycle: no deadlock. One copy of each resource: a cycle means deadlock. Several copies: a cycle is needed but not enough, so a detector checks whether some thread can still finish.
What happens
;; step 1/10 · symbols thread a circle T1, T2 resource a box lock A, lock B copy a dot in a box one copy each (a lock is one copy) arrows (directed edges) come next: resource -> thread assignment: the thread holds a copy thread -> resource request: the thread waits for a copy
Textbooks draw both kinds of arrow as plain solid arrows; the direction alone tells them apart. The figure dashes request arrows to make the waiting easier to see. The card shortens pthread_mutex_lock(&A) to lock(A) and shows the four calls in the order they happen. In the whole program, T1 and T2 each sleep for a second after their first lock, so this order is almost certain; without the sleep the threads might not deadlock on a given run.