title: How can I make std::thread not struck the pragma?
body: <p>I want to design a <code>timer</code> class, there is a function, which sleep some seconds, then call other function.</p>
  <p>please see the code:</p>
  <pre><code>#include &lt;thread&gt;
  #include &lt;iostream&gt;
  
  void func() { printf(&quot;timer thread function called\n&quot;); }
  
  class Timer {
   public:
    template &lt;typename Fn&gt; 
    void sleep_start(int sec, const Fn&amp; f) {
      printf(&quot;sleep %d\n&quot;, sec);
      td_ = std::thread([sec, f]() { std::this_thread::sleep_for(std::chrono::seconds(sec)); f(); }); 
      if (td_.joinable()) td_.join();
    }
    std::thread td_;
  };
  
  class A { 
   public:
    void start() {
      t_.sleep_start(10, func);
      printf(&quot;start function\n&quot;);
    }
    Timer t_; 
  };
  
  int main() {
    A a;
    a.start();
  }
  </code></pre>
  <p>this code can work well, but the <code>sleep_start</code> function stuck the program.</p>
  <p>the real output is:</p>
  <pre><code>sleep 10
  timer thread function called
  start function
  </code></pre>
  <p>the ideal output is:</p>
  <pre><code>sleep 10
  start function
  timer thread function called
  </code></pre>
  <p>could you help on this? how to make the thread function not struck the program?</p>