title: How to wake a std::thread while it is sleeping
body: <p>I am using C++11 and I have a <code>std::thread</code> which is a class member, and it sends information to listeners every 2 minutes. Other that that it just sleeps. So, I have made it sleep for 2 minutes, then send the required info, and then sleep for 2 minutes again.</p>
  
  <pre><code>// MyClass.hpp
  class MyClass {
  
      ~MyClass();
      RunMyThread();
  
  private:
      std::thread my_thread;
      std::atomic&lt;bool&gt; m_running;
  }
  
  
  MyClass::RunMyThread() {
  
      my_thread = std::thread { [this, m_running] {
      m_running = true;
      while(m_running) {
          std::this_thread::sleep_for(std::chrono::minutes(2));
          SendStatusInfo(some_info);
      }
  }};
  }
  
  // Destructor
  ~MyClass::MyClass() {
      m_running = false; // this wont work as the thread is sleeping. How to exit thread here?
  }
  </code></pre>
  
  <p><strong>Issue:</strong><br>
  The issue with this approach is that I cannot exit the thread while it is sleeping. I understand from reading that I can wake it using a <code>std::condition_variable</code> and exit gracefully? But I am struggling to find a <strong>simple example</strong> which does the bare minimum as required in above scenario. All the <code>condition_variable</code> examples I've found look too complex for what I am trying to do here.</p>
  
  <p><strong>Question:</strong><br>
  How can I use a <code>std::condition_variable</code> to wake the thread and exit gracefully while it is sleeping? Or are there any other ways of achieving the same without the <code>condition_variable</code> technique?</p>
  
  <p>Additionally, I see that I need to use a <code>std::mutex</code> in conjunction with <code>std::condition_variable</code>? Is that really necessary? Is it not possible to achieve the goal by adding the <code>std::condition_variable</code> logic only to required places in the code here?</p>
  
  <p><strong>Environment:</strong><br>
  Linux and Unix with compilers gcc and clang.</p>