title: Run and break an infinite loop using two threads
body: <p>I am trying to run a loop until the user chooses to break out of it. Whether the user wants to run the function all night or for just a few seconds the loop should repeat until the user decides to stop it.</p>
  
  <p>In researching solutions I came across using two threads to achieve this. The first thread would run the infinite loop while the second thread waited for user input. Upon receiving that input the second thread would terminate the first and then return.</p>
  
  <ul>
  <li>How do I use the second thread to terminate the first?</li>
  </ul>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;ctime&gt;
  #include &lt;thread&gt;
  #include &lt;cstdlib&gt;
  #include &lt;Windows.h&gt;
  
  using namespace std;
  
  void timeCount()
  {
      time_t rawTime;
      struct tm * timeinfo;
  
      do
      {
          Sleep(500);
          system("cls");
          time(&amp;rawTime);
          cout &lt;&lt; "Seconds passed this epoch:" &lt;&lt; rawTime &lt;&lt; endl &lt;&lt; endl;
          timeinfo = localtime(&amp;rawTime);
          cout &lt;&lt; "The local time is:" &lt;&lt; asctime(timeinfo) &lt;&lt; endl;
          timeinfo = gmtime(&amp;rawTime);
          cout &lt;&lt; "The UTC time is :" &lt;&lt; asctime(timeinfo) &lt;&lt; endl;
      } while (1 != 0);
  
  };
  
  void getStop()
  {
      system("pause");
  };
  
  void timeSince()
  {
      thread counter(timeCount);
      thread stopper(getStop);
      counter.detach();
      stopper.join();
  
      counter.~thread();
  };
  </code></pre>