title: Best practices for lock (or Enter/Exit) in C#
body: <p>I have a measuring instrument object:</p>
  
  <pre><code>public class Instrument
  {
    public double Measure() 
    { 
      return 0; 
    }
  }
  </code></pre>
  
  <p>I have a device that needs to do some measuring:</p>
  
  <pre><code>public class Device
  {
    public Instrument MeasuringInstrument { get; set; }
    public void DoMeasuring()
    {
      var result = this.MeasuringInstrument.Measure();
    }
  }
  </code></pre>
  
  <p>The measuring instrument can only operate on one device at a time, yet many devices may use the same instrument. I'm new to threading, and from what I understand, both of the following solutions have caveats.</p>
  
  <pre><code>public class Instrument
  {
    public double Measure() 
    { 
      lock(this)
      {
        return 0; 
      }
    }
  }
  
  public class Device
  {
    public Instrument MeasuringInstrument { get; set; }
    public void DoMeasuring()
    {
      lock(this.MeasurementInstrument)
      {
        var result = this.MeasuringInstrument.Measure();
      }
    }
  }
  </code></pre>
  
  <p>I've read it's best to lock on private objects, but I don't know how to do that while still allowing the MeasuringInstrument to be get/set on the Device. Any suggestions?</p>
  
  <p>Thanks much,<br />
  Ken</p>