title: Multithreading in multi cpu multithreading
body: <p>Suppose I have an undirected graph.</p>
  
  <p>A small portion of the graph : </p>
  
  <pre><code>A -----\
          C
  B -----/
  </code></pre>
  
  <p>Now the node A and B proceeds to modify parallely node C. // Node A and Node B process Node C in parallel thread.</p>
  
  <p>I want the thread for Node B will wait for thread for Node A to complete processing.
  It has to be this way. No other way possible.</p>
  
  <p>I also don't want other thread that are processing other Nodes to wait for the above condition, ie, I don't want to use mutex.</p>
  
  <p>I have created a class member boolean flag and want to use that as a condition. // let that 
  flag be d_flag</p>
  
  <p><strong>MT here uses multiple CPUs.</strong></p>
  
  <p>Solution that I think doesn't works :</p>
  
  <h2>CPU1 : Thread 1</h2>
  
  <pre><code>while (d_flag) {
      // busy waiting
  }
  d_flag = true;
  
  // Critical Section
  
  d_flag = false;
  </code></pre>
  
  <h2>CPU2 : Thread 1</h2>
  
  <pre><code>while (d_flag) {
     // busy waiting
  }
  
  d_flag = true;
  
  // Critical Section
  
  d_flag = false;
  </code></pre>
  
  <p><strong>My understanding is that if in multiple CPU, Node A thread is running on CPU1 and Node B thread on CPU 2, the spin lock is defeated.</strong></p>
  
  <p>There may be a condition where both the while loop evaluates to false as the test in the while loop is no longer atomic.</p>
  
  <p><strong>Is my understanding correct????? If yes, is there is a solution without using overall mutex.</strong></p>