#ifndef TIMER_HPP #define TIMER_HPP #include #include #include #include class Timer { std::atomic active{true}; public: void setTimeout(auto function, int delay); void setInterval(auto function, int interval); void stop(); }; void Timer::setTimeout(auto function, int delay) { active = true; std::thread t([this, function, delay]() { if(!active.load()) return; std::this_thread::sleep_for(std::chrono::milliseconds(delay)); if(!active.load()) return; function(); }); t.detach(); } void Timer::setInterval(auto function, int interval) { active = true; std::thread t([this, function, interval]() { while(active.load()) { std::this_thread::sleep_for(std::chrono::milliseconds(interval)); if(!active.load()) return; function(); } }); t.detach(); } inline void Timer::stop() { active = false; } #endif //TIMER_HPP