title: How to ensure a certain method doesn't get executed if some other method is running?
body: <p>Let's say I have two methods - </p>
  
  <pre><code>getCurrentValue(int valueID)
  updateValues(int changedComponentID)
  </code></pre>
  
  <p>These two methods are called on the same object independently by separate threads. </p>
  
  <p><code>getCurrentValue()</code> simply does a database look-up for the current valueID. </p>
  
  <p>"<code>Values</code>" change if their corresponding <code>components</code> change. The <code>updateValues()</code>method updates those values that are dependent upon the component that just changed, i.e. <code>changedComponentID</code>. This is a database operation and takes time. </p>
  
  <p>While this update operation is going on, I do not want to return a stale value by doing a lookup from the database, but I want to wait till the update method has completed. At the same time, I don't want two update operations to happen simultaneously or an update to happen when a read is going on. </p>
  
  <p>So, I'm thinking of doing it this way - </p>
  
  <pre><code>[MethodImpl(MethodImplOptions.Synchronized)]
  public int getCurrentValue(int valueID)
       {
          while(updateOperationIsGoingOn)
            {
               // do nothing  
            }
          readOperationIsGoingOn = true;
  
          value = // read value from DB
  
          readOperationIsGoingOn = false; 
          return value;
       }
  
  [MethodImpl(MethodImplOptions.Synchronized)]
  public void updateValues(int componentID)
       {
           while(readOperationIsGoingOn)
            {
               // do nothing
            }
           updateOperationIsGoingOn = true;
  
           // update values in DB
  
           updateOperationIsGoingOn = false;
       }
  </code></pre>
  
  <p>I'm not sure whether this is a correct way of doing it. Any suggestions? Thanks. </p>