1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-14 15:39:26 +08:00
Files
Alas/blcrack/cracker/timer.hpp
2025-11-01 00:23:46 +08:00

47 lines
997 B
C++

#ifndef TIMER_HPP
#define TIMER_HPP
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
class Timer {
std::atomic<bool> 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