It is quit difficult to prove that code is free from deadlocks situations, but there is possible to write deadlock-free code and also it is important to do that when writing the code. There are few simple rules as below:
● Lock ordering is vital. Nested locks must always be in the same order which can prevents the deadlock. Document lock ordering so others will follow it.
● Prevent starvation. Make sure, this code (writing code) will always finish?
● No self lock. Do not try to acquire the same lock which is already locked by same thread.
● Simplicity in design, Complexity of locking orders in your code will invites deadlocks, so follow simplicity in locking.