title: Synchronize three Threads in C++
body: <p>I have the following program (made up example!):</p>
  
  <pre><code>#include&lt;thread&gt;
  #include&lt;mutex&gt;
  #include&lt;iostream&gt;
  
  class MultiClass {
      public:
          void Run() {
              std::thread t1(&amp;MultiClass::Calc, this);
              std::thread t2(&amp;MultiClass::Calc, this);
              std::thread t3(&amp;MultiClass::Calc, this);
              t1.join();
              t2.join();
              t3.join();
          }
      private:
          void Calc() {
              for (int i = 0; i &lt; 10; ++i) {
                  std::cout &lt;&lt; i &lt;&lt; std::endl;
              }
          }
  };
  
  int main() {
      MultiClass m;
      m.Run();
      return 0;
  }
  </code></pre>
  
  <p>What I need is to sync the loop iterations the following way and I cant come up with a solution (I've been fiddling for about an hour now using mutexes but cant find THE combination):
  <code>t1</code> and <code>t2</code> shall do one loop iteration, then <code>t3</code> shall do one iteration, then again <code>t1</code> and <code>t2</code> shall do one, then <code>t3</code> shall do one.</p>
  
  <p>So you see, I need <code>t1</code> and <code>t2</code> to do things simultaneously and after one iteration, <code>t3</code> shall do one iteration on its own.</p>
  
  <p>Can you point your finger on how I would be able to achieve that? Like I said, ive been trying this with mutexes and cant come up with a solution.</p>