Introduced to me through Hemal Shah.
Livelock occurs when two or more processes continually repeat the same interaction in response to changes in the other processes without doing any useful work.
How is this different from deadlock?
In a deadlock all processes are in the waiting state, NOT in the running state.
A livelock happens when two threads keep taking actions in response to the other thread instead of making any progress.
Example of Livelock
The best analogy is to think of two persons trying to cross each other in a hallway. John moves to the left to let Arun pass, and Arun moves to his right to let John pass.
Both block each other now. John sees he’s now blocking Arun and moves to his right and Arun moves to his left seeing he’s blocking John. They never cross each other and keep blocking each other. This scenario is an example of a livelock.
Taken from Educative.io
How to avoid livelocks?
Livelocks can be avoided by making use of ReentrantLock as a way to determine which thread has been waiting longer so that you can assign it a lock.
Um we don’t have that in C++?
As a best practice, don’t block locks; if a thread can’t acquire a lock, it should release previously acquired locks to try again later.