title: String Alternation on 2 process C++
body: <p>I am trying to conduct strict alternation on 2 processes, but I am not sure how to declare the critical region and non-critical region. Here is the code that I have:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include &lt;pthread.h&gt;
  
  
  int count;
  int turn = 0;   //  Shared variable used to implement strict alternation
  
  
  void* myFunction(void* arg)
  {
      int actual_arg = *((int*) arg);
  
      for(unsigned int i = 0; i &lt; 10; ++i) {
  
        while(1)
        {
            while(turn != 0)
            {
                critical_region_0();
                turn = 1;
                non_critical_region_0();
            }
        }
  
        //  Beginning of the critical region
  
        count++;
        std::cout &lt;&lt; "Thread #" &lt;&lt; actual_arg &lt;&lt; " count = " &lt;&lt; count &lt;&lt; 
        std::endl;
  
        //  End of the critical region
  
        while(0)
        {
            while(turn != 1)
            {
                critical_region_1();
                turn = 0          
                non_critical_region_1();
            }
        }
  
  
      }
  
      pthread_exit(NULL);
  }
  
  int main()
  {
      int rc[2];
      pthread_t ids[2];
      int args[2];
  
      count = 0;
      for(unsigned int i = 0; i &lt; 2; ++i) {
          args[i] = i;
          rc[i] = pthread_create(&amp;ids[i], NULL, myFunction, (void*) &amp;args[i]);
      }
  
      for(unsigned int i = 0; i &lt; 2; ++i) {
          pthread_join(ids[i], NULL);
      }
  
      std::cout &lt;&lt; "Final count = " &lt;&lt; count &lt;&lt; std::endl;
      pthread_exit(NULL);
      }
  </code></pre>
  
  <p>I know that the critical region and non-critical regions are written as if they are a method but I am using those as placeholders. Is there a way to conduct Strict Alternation without the use of these methods?</p>
  
  <p>Here is what the output should look like.</p>
  
  <pre><code>Thread #0 count = 1
  Thread #1 count = 2
  Thread #0 count = 3
  Thread #1 count = 4
  Thread #0 count = 5
  Thread #1 count = 6
  Thread #0 count = 7
  Thread #1 count = 8
  Thread #0 count = 9
  Thread #1 count = 10
  Thread #0 count = 11
  Thread #1 count = 12
  Thread #0 count = 13
  Thread #1 count = 14
  Thread #0 count = 15
  Thread #1 count = 16
  Thread #0 count = 17
  Thread #1 count = 18
  Thread #0 count = 19
  Thread #1 count = 20
  Final count = 20
  </code></pre>
  
  <p>The output I can only manage to get is all of thread 1 first then thread 0.</p>