Q:

Avoid deadlocks in a multithreaded process

What are the best practices/idioms should someone follow in order to avoid deadlocks? 

A:

Please see What are common reasons for deadlocks?

A:

There are four conditions which must occur for deadlock to occur:

Mutual exclusion condition: a resource that cannot be used by more than one process at a time
Hold and wait condition: processes already holding resources may request new resources
No preemption condition: No resource can be forcibly removed from a process holding it, resources can be released only by the explicit action of the process
Circular wait condition: two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds

Avoid at least one of these, and preferably more, and you shouldn't have too many problems. 

A:

There is so called Banker's algorithm, for deadlock avoidance. Also you can consider the use of Watch Dog in order to break out form deadlock. Here also few interesting points. 

