title: c# locking
body: <p>I am trying to prevent data races in a multihreaded server. My problem is the following: there is a <code>List&lt;RServer&gt;</code>, the type <code>RServer</code> is a class with several fields. Now, the server has several threads all running at the same time and they can modify both the <code>List</code> (adding more items) and the individual <code>RServer</code> instances (changing the fields).</p>
  
  <p>So my strategy is to make a <code>readonly object RServerLock = new object( )</code> in each of the <code>RServer</code> instances and additionally a <code>readonly object RServerListLock = new object( )</code> and enclose all the code that modifies either (the <code>List</code> or a <code>RServer</code> instance) in a <code>lock</code>. Is this safe? What happens if a thread tries to lock a <code>RServerLock</code> <em>while</em> another one is locking it?</p>