I am struggling to explain \"deadlock\" in threads in easy words, so please help. What could be the best example of \"deadlock\" (say, in Java), and how it does happen in st
Thrd 1 --- Lock A - atmpt lock on B -
\ / \
\ / \
\ / \
--- Lock A / --- wait for lock on B
Thrd 2--- Lock B - atmpt lock on A -
\ / \
\ / \
\ / \
--- Lock B / --- wait for lock on A
Thread 1 runs, Locks A, does some stuff, and gets interrupted by Thread 2 which Locks B, does some stuff and gets interrupted by Thread 1 which attempt to Lock B, but thread 2 has locked B so thread 1 waits, and is interrupted by Thread 2 which attempts to lock A, but thread 1 has lock on A so Thread 2 has to wait.
Both threads are waiting for the other thread to release a lock on a resource they are trying to get a lock on...
Deadlock
Jack and Jill happens to want to make a sandwich at the same time. Both need a slice of bread, so they both goes to get the loaf of bread and a knife.
Jack gets the knife first, while Jill gets the loaf of bread first. Now Jack tries to find the loaf of bread and Jill tries to find the knife, but both find that what they need to finish the task is already in use. If they both decide to wait until what they need is no longer in use, they will wait for each other forever. Deadlock.
Deadlock is when two threads wait on each other, neither can proceed until the other does first, and so both are stuck.
Deadlocking requires at least 2 locks, and both threads have to contain code that takes locks and also waits for locks to be released.
Thread 1 has lock A and wants lock B, so it waits for lock B to be released.
Thread 2 has lock B and wants lock A, so it waits for lock A to be released.
Now you have a deadlock. Both threads a waiting for a lock, so neither is executing, so neither can release the lock that the other is waiting for.
(Slightly over-simplified) There are two people, screwing nuts onto bolts.
The procedure (same for both) is:
So what happens when there is just a nut and a bolt left? The first person picks up a nut, the second grabs a bolt. So far so good, but now they are stuck, each having a resource the other needs.
Without special instructions they will sit there deadlocked forever.
Or you could just show them this video
A lock chain occurs when a worker is blocked by another worker. A cannot continue because of B. The chain can be longer: A is blocked by B, B is blocked by C, C is blocked by D.
A deadlock is when the lock chain forms a loop. A is blocked by B, B by C, C by A and the chain had formed a loop, no progress is possible.
The typical way of preventing deadlocks is to use lock hierachies: if locks are always aquired by every worker in the same order, then deadlocks are not possible because each blocking occurs between a worker than holds locks ranked X and waits for resources ranked Y, where X > Y always. A loop cannot form in this case, as it would require at least one worker to go against the hierarchy in order to close the loop. That how the theory goes, at least. In prcatice is very very hard to come up with realistic hierarchies (and no, address of resource does not work).
If deadlocks cannot be avoided (eg. database systems) then the solution is to have dedicated threads that check the deadlock chains in search of loops and kill one of the participants to free the loop.
Easiest way is for two different threads to try to get two locks in different orders:
thread 1:
lock(a)
lock(b)
thread2:
lock(b)
lock(a)
Assume that thread 1 gets lock A and then goes to sleep. Thread 2 gets lock B and then attempts to get lock A; since lock A is taken, thread 2 will be put to sleep until thread A is unlocked. Now thread 1 wakes back up and tries to get lock B and will be put to sleep.
For this case, there are a couple of ways to prevent it: