title: Stop infinite loop in different thread
body: <p>In my application I am using multiple threads for some time consuming task, that is intended to run for the whole duration f the application. What I am trying to achieve is some nice and clean approach to kill the loop in second thread just before exiting app, so I can clean after loop and then close everything.</p>
  
  <p>So far I've come up with this approach, but to be honest I am not huge fan of it:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  bool stop_flag;
  
  void foo()
  {
      while(!stop_flag)
      {
          //do something
      }
      clean_after();
  }
  
  int main()
  {
      stop_flag = false;
      std::thread whileThread(foo);
  
      // app lifetime
  
      stop_flag = true;   
      whileThread.join();
      return 0;
  }
  </code></pre>