title: C++11 std::threads and waiting for threads to finish
body: <p>I have a vector of Timer Objects. Each Timer Object launches an std::thread that simulates a growing period. I am using a Command pattern.</p>
  
  <p>What is happening is each Timer is getting executed one after another but what I really want is for one to be executed....then once finished, the next one...once finished the next...while not interfering with the main execution of the program</p>
  
  <pre><code>class Timer 
  {
      public:
  
          bool _bTimerStarted;
          bool _bTimerCompleted;
  
          int _timerDuration;
  
          virtual ~Timer() { }
          virtual void execute()=0;
          virtual void runTimer()=0;
  
          inline void setDuration(int _s) { _timerDuration = _s; };
          inline int getDuration() { return _timerDuration; };
  
          inline bool isTimerComplete() { return _bTimerCompleted; };
  };
  
  class GrowingTimer : public Timer
  {
      public:
          void execute()
          {
              //std::cout &lt;&lt; "Timer execute..." &lt;&lt; std::endl;
  
              _bTimerStarted = false;
              _bTimerCompleted = false;
  
              //std::thread t1(&amp;GrowingTimer::runTimer, this); //Launch a thread
              //t1.detach();
  
              runTimer();
          }
  
          void runTimer()
          {
              //std::cout &lt;&lt; "Timer runTimer..." &lt;&lt; std::endl;
  
              _bTimerStarted = true;
  
              auto start = std::chrono::high_resolution_clock::now();
              std::this_thread::sleep_until(start + std::chrono::seconds(20));
  
              _bTimerCompleted = true;
  
              std::cout &lt;&lt; "Growing Timer Finished..." &lt;&lt; std::endl; 
          }
  };
  
  class Timers
  {
      std::vector&lt;Timer*&gt; _timers;
  
      struct ExecuteTimer
      {
          void operator()(Timer* _timer) { _timer-&gt;execute(); }
      };
  
      public:
          void add_timer(Timer&amp; _timer) { _timers.push_back(&amp;_timer); }
  
          void execute()
          {
              //std::for_each(_timers.begin(), _timers.end(), ExecuteTimer());
  
              for (int i=0; i &lt; _timers.size(); i++)
              {
                   Timer* _t = _timers.at(i);
                  _t-&gt;execute();
  
                  //while ( ! _t-&gt;isTimerComplete())
                  //{
  
                  //}
              }
          }
  };
  </code></pre>
  
  <p>Executing the above like:</p>
  
  <pre><code>Timers _timer;
  GrowingTimer _g, g1;
  
  _g.setDuration(BROCCOLI::growTimeSeconds);
  _g1.setDuration(BROCCOLI::growTimeSeconds);
  
  _timer.add_timer(_g);
  _timer.add_timer(_g1);
  
  start_timers();
  
  }
  
  void start_timers() 
  {
      _timer.execute();
  }
  </code></pre>
  
  <p>In Timers::execute I am trying a few different ways to execute the first and not execute the 
  next until I somehow signal it is done.</p>
  
  <p>UPDATE:</p>
  
  <p>I am now doing this to execute everything:</p>
  
  <pre><code>Timers _timer;
  GrowingTimer _g, g1;
  
  _g.setDuration(BROCCOLI::growTimeSeconds);
  _g1.setDuration(BROCCOLI::growTimeSeconds);
  
  _timer.add_timer(_g);
  _timer.add_timer(_g1);
  
  //start_timers();
  
  std::thread t1(&amp;Broccoli::start_timers, this); //Launch a thread
  t1.detach();
  
  }
  
  void start_timers() 
  {
      _timer.execute();
  }
  </code></pre>
  
  <p>The first time completes (I see the "completed" cout), but crashes at <code>_t-&gt;execute();</code> inside the <code>for loop</code> with an EXEC_BAD_ACCESS. I added a cout to check the size of the vector and it is 2 so both timers are inside. I do see this in the console: </p>
  
  <pre><code>this    Timers *    0xbfffd998
  _timers std::__1::vector&lt;Timer *, std::__1::allocator&lt;Timer *&gt; &gt;
  </code></pre>
  
  <p>if I change the <code>detach()</code> to <code>join()</code> everything completes without the crash, but it blocks execution of my app until those timers finish.</p>