Codementor Events

Deadlocks and Livelocks - plain English

Published Mar 28, 2018Last updated Mar 13, 2020
Deadlocks and Livelocks - plain English

Both deadlocks and livelocks can be the death of a multithreaded application. There are many technical explanations as to what they are but I think the following plain english examples describe them wonderfully. I also apologise to any meat haters in advance.

Deadlock

My 2 sons named Thread1 and Thread2, sit together at the dinner table. I bring out a fat juicy steak and place it between them. I then place 1 fork and 1 knife on either side of the plate. Thread1 quickly grabs the fork, whilst at the same moment my other son, Thread2 grabs the knife. So there they sit, Thread1 with his fork desperately needing Thread2's knife, and Thread2 in dire need of Thread1's fork. Neither can eat the steak (not in a civilised manner at least), without the others utensil - they are in a deadlock. Thread1 has something Thread2 needs, and Thread2 has something Thread1 needs - neither can progress. Of course, I ate the steak myself whilst they stared at each other 😃

Livelock

Now its time for dessert 😃 I feel bad about eating the steak so I clean the table, and bring out a lovely dessert and place 1 spoon next to it. Thread1 picks up the spoon, and being very polite, gives it to his brother Thread2 so that he may take the first taste of the dessert. Thread2 is equally as polite and promptly passes the spoon back to Thread1. Thread1 thanks him but passes the spoon back to Thread2 - the spoon goes back and forth between them with neither getting to enjoy the dessert. Where they both keep yielding to the other is known as a livelock.

Discover and read more posts from Steve Donovan
get started
post commentsBe the first to share your opinion
Show more replies