samedi 29 août 2020

Timer calling a function at regular interval interval

static void timer_start(std::function<void(void)> func, unsigned int interval)
{
  
  std::thread([func, interval]()
  { 
    while (true)
    { 
      auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(interval);
      func();
      std::this_thread::sleep_until(x);
    }
  }).detach();
}

static void interfacetoBackend()
{ 

}

int main(){
    timer_start(interfacetoBackend, 2000);  

}

I have a timer calling a function at regular interval as shown above. I like to stop before the program end. Currently, I can't stop the time. How can I stop the time call?

Aucun commentaire:

Enregistrer un commentaire