title: Locking strategies and techniques for preventing deadlocks in code
body: <p>The common solution to preventing deadlock in code is to make sure the sequence of locking occur in a common manner regardless of which thread is accessing the resources.</p>
  
  <p>For example given threads T1 and T2, where T1 accesses resource A and then B and T2 accesses resource B and then A. Locking the resources in the order they are needed causes a dead-lock. The simple solution is to lock A and then lock B, regardless of the order specific thread will use the resources.</p>
  
  <p>Problematic situation:</p>
  
  <pre><code>Thread1                         Thread2
  -------                         -------
  Lock Resource A                 Lock Resource B
   Do Resource A thing...          Do Resource B thing...
  Lock Resource B                 Lock Resource A
   Do Resource B thing...          Do Resource A thing...
  </code></pre>
  
  <p>Possible Solution:</p>
  
  <pre><code>Thread1                         Thread2
  -------                         -------
  Lock Resource A                 Lock Resource A
  Lock Resource B                 Lock Resource B
   Do Resource A thing...          Do Resource B thing...
   Do Resource B thing...          Do Resource A thing...
  </code></pre>
  
  <p>My question is what other techniques, patterns or common practices are used in coding to guarantee dead lock prevention?</p>