title: Sample code to illustrate a deadlock by using lock(this)
body: <p>I've read several articles and posts that say that <code>lock(this)</code>, <code>lock(typeof(MyType))</code>, <code>lock("a string")</code> are all bad practice because another thread could lock on the same key and cause a deadlock.  In order to understand this problem, I was trying to create some sample code to illustrate the deadlock but have been unable to wrap my head around this. </p>
  
  <p>Can someone write a concise bit of code that illustrates this classic problem?  Please keep it short, I can digest code in smaller chunks only.  </p>
  
  <p><B>Edit:</B>
  I think lassevk sums it up well; that the real problem is that you have lost control over your locks.  Once that happens, you cannot control the order the locks are called, and you are allowing a potential deadlock situation.  </p>
  
  <p><code>lock(this)</code>, <code>lock(typeof(MyType))</code>, etc all are situations where you have chosen a lock that is impossible to control.</p>