title: C++ Non-blocking async timer
body: <p>I've read many posts about this, but didn't find the answer to this question.
  I want to make a timer class which works in background and when finished do something. And even I want to call multiple timers at once which are counts down asynchronously.</p>
  
  <p>I've found this class while I was searching about it, which seems working, but not in background.How can I transform it to do what I want?</p>
  
  <p>Timer.h:</p>
  
  <pre><code>#ifndef TIMER_H
  #define TIMER_H
  
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  
  class Timer
  {
  public:
      typedef std::chrono::milliseconds Interval;
      typedef std::function&lt;void(void)&gt; Timeout;
  
      Timer(const Timeout &amp;timeout);
      Timer(const Timeout &amp;timeout,
            const Interval &amp;interval,
            bool singleShot = true);
  
      void start(bool multiThread = false);
      void stop();
  
      bool running() const;
  
      void setSingleShot(bool singleShot);
      bool isSingleShot() const;
  
      void setInterval(const Interval &amp;interval);
      const Interval &amp;interval() const;
  
      void setTimeout(const Timeout &amp;timeout);
      const Timeout &amp;timeout() const;
  
  private:
      std::thread _thread;
  
      bool _running = false;
      bool _isSingleShot = true;
  
      Interval _interval = Interval(0);
      Timeout _timeout = nullptr;
  
      void _temporize();
      void _sleepThenTimeout();
  };
  
  #endif // TIMER_H
  enter code here
  </code></pre>
  
  <p>Timer.cpp:</p>
  
  <pre><code>#include "Timer.h"
  
  Timer::Timer(const Timeout &amp;timeout)
      : _timeout(timeout)
  {
  }
  
  Timer::Timer(const Timer::Timeout &amp;timeout,
               const Timer::Interval &amp;interval,
               bool singleShot)
      : _isSingleShot(singleShot),
        _interval(interval),
        _timeout(timeout)
  {
  }
  
  void Timer::start(bool multiThread)
  {
      if (this-&gt;running() == true)
          return;
  
      _running = true;
  
      if (multiThread == true) {
          _thread = std::thread(
                      &amp;Timer::_temporize, this);
      }
      else{
          this-&gt;_temporize();
      }
  }
  
  void Timer::stop()
  {
      _running = false;
      _thread.join();
  }
  
  bool Timer::running() const
  {
      return _running;
  }
  
  void Timer::setSingleShot(bool singleShot)
  {
      if (this-&gt;running() == true)
         return;
  
      _isSingleShot = singleShot;
  }
  
  bool Timer::isSingleShot() const
  {
      return _isSingleShot;
  }
  
  void Timer::setInterval(const Timer::Interval &amp;interval)
  {
      if (this-&gt;running() == true)
         return;
  
      _interval = interval;
  }
  
  const Timer::Interval &amp;Timer::interval() const
  {
      return _interval;
  }
  
  void Timer::setTimeout(const Timeout &amp;timeout)
  {
      if (this-&gt;running() == true)
         return;
  
      _timeout = timeout;
  }
  
  const Timer::Timeout &amp;Timer::timeout() const
  {
      return _timeout;
  }
  
  void Timer::_temporize()
  {
      if (_isSingleShot == true) {
          this-&gt;_sleepThenTimeout();
      }
      else {
          while (this-&gt;running() == true) {
              this-&gt;_sleepThenTimeout();
          }
      }
  }
  
  void Timer::_sleepThenTimeout()
  {
      std::this_thread::sleep_for(_interval);
  
      if (this-&gt;running() == true)
          this-&gt;timeout()();
  }
  </code></pre>