title: printing odd and even number printing alternately using threads in C++
body: <p><a href="https://stackoverflow.com/questions/6017281/odd-even-number-printing-using-thread">Odd even number printing using thread</a>  I came across this question and wanted to discuss solution in C++ . What I can think of using 2 binary semaphores odd and even semaphore. even semaphore initialized to 1 and odd initialized to 0.    </p>
  
  <pre><code>**T1 thread function** 
  funOdd()
  {  
    wait(even)  
    print odd;  
    signal(odd)  
  }
  
  
  **T2 thread function**
  funEven()  
  {  
    wait(odd)  
    print even  
    signal(even)  
  }  
  </code></pre>
  
  <p>In addition to this if my functions are generating only number and there is a third thread T3 which is going to print those numbers then what should be ideal design ? I used an array where odd number will be placed at odd place and even number will be place at even position. T3 will read from this array this will avoid any thread saftey over this array and if T3 does not find any index then it will wait till that index gets populated.  Another solution can be to use a queue which will have a mutex which can be used by T1 and T2 while insertion. </p>
  
  <p>Please comment on this solution and how can i make it more efficient.  </p>
  
  <p><strong>Edit to make problem much clear</strong>: Overall problem is that I have two producers (T1,T2) and a single consumer (T3), and my producers are interdependent. </p>