title: how to solve busy waiting for many running threads
body: <p>I have write a multithread program :</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;process.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  using namespace std;
  ofstream myfile;
  BYTE lockmem=0x0;
  unsigned int __stdcall mythreadA(void* data) 
  {
      __asm
      {
          mov DL,0xFF
          mutex:
          mov AL,0x0
          LOCK CMPXCHG lockmem,DL
          jnz mutex
      }
  
      // Enter Critical Section
      for (int i = 0; i &lt; 100000; i++) 
      {
          myfile &lt;&lt; "." &lt;&lt; i &lt;&lt; endl;
      }
      // Exit Critical Section
      __asm
      {
      lock not lockmem
      }
      return 0;
  }
  
  int main(int argc, char* argv[])
  {
      myfile.open ("report.txt");
      HANDLE myhandleA[10];
      //int index = 0;
      for(int index = 0;index &lt; 100;index++)
      {
          myhandleA[index] = (HANDLE)_beginthreadex(0, 0, &amp;mythreadA, 0, 0, 0);
      }
      getchar();
      myfile.close();
      return 0;
  }
  </code></pre>
  
  <p>at the critical section I write inline code to sure that only one thread is in the critical section .( in this program I don't want to use API and functions for implement the only one thread in critical section so I use inline assembly ). now I have busy waiting problem .because after one thread enter in the critical section the other threads are busy in the loop before critical section , so the process of cpu go up and up! here I search for ways to solve the problem of busy waiting. ( I prefer to use assembly instruction instead of API and any functions but I also want to know them)</p>