title: Partial thread lock: mix of thread-safe and unsafe code
body: <p>I have two methods, say, <code>read()</code> and <code>init()</code>.</p>
  
  <p><code>read</code> is re-entrant and thread-safe; it can (and should) be executed in parallel by multiple threads.</p>
  
  <p>However, any other mix of them is not thread-safe; e.g. <code>read</code> must block if <code>init</code> is being executed and vice versa, not to mention <code>init</code> on itself. (Things are complicated because it's all a wrapper to unmanaged code).</p>
  
  <p>How do I organise locks in this situation (in .net, C# or VB)?</p>
  
  <p><code>init</code> on itself seems trivial: I can wrap it in the standard <code>lock ()</code> / <code>SyncLock</code> mechanism. But is there are a similar simple mechanism that allows <code>read</code> to:</p>
  
  <ol>
  <li>Block on the same lock object used for <code>init</code> but not acquire exclusivity on it (otherwise <code>read</code> will block itself on it).</li>
  <li>When block is released, flag another object (a mutex?) on which <code>init</code> should block in addition to its own lock (but here in <code>read</code> we don't check it).</li>
  <li>Run the code.</li>
  <li>Release the second object.</li>
  </ol>
  
  <p>As often happens in multi-threading, there must be many pitfalls I'm not very familiar with. For example, I see the read-lock object may be flagged multiple times by multiple reads running in parallel, and must fully unwind before <code>init</code> can go. It all feels like a fairly common situation and there must be standard ways of dealing with it...</p>