title: How to terminate or stop a detached thread in c++?
body: <p>I am interested in terminating/stopping/killing a detached thread in c++. How can this be done?</p>
  
  <pre><code>void myThread()
  {
      int loop = 0;
      while(true)
      {
          std::this_thread::sleep_for(std::chrono::seconds(5));
          ++loop;
      }
  }
  
  void testThread()
  {
      std::thread globalThread(myThread);
      globalThread.detach();
  }
  
  int main(void)
  {
      testThread();
      for(unsigned int i=0; i &lt; 1000; i++)
      {
          cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>The reason why I'd like to "stop"/"terminate" the globalThread() is because valgrind lists that this is a "possibly lost" type of memory leak (152 bytes). What is the best way to deal with this?</p>