Livelock
Livelock occurs when two or more processes continuously change their state in response to changes in the other process(es) without doing any useful work.
Introduced to me through Hemal Shah.
How is this different from deadlock?
In a livelock, all processes are in the running state, NOT in the waiting state. This is why livelocks are much harder to identify, because they are in the running state.
Resources
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
Solution
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.
you know how in deadlock, the conditions are mutual exclusion, no preemption, hold and wait and circular set. What about for livelock?
Mutual exclusion, depdent conditions, no progress and continual response.