Check Timeout
#include <chrono>
class timeout_trigger
{
public:
timeout_trigger() : busy(false), auto_reset(true), last_tick(std::chrono::system_clock::now()) {}
void reset() { busy = false; }
void enable_auto_reset(bool v) { auto_reset = v; }
bool is_over_limit(int limit_sec) {
using namespace std::chrono;
if (!busy) {
busy = true;
last_tick = system_clock::now();
}
auto du = duration_cast<seconds>(system_clock::now() - last_tick).count();
if (du > limit_sec) {
if (auto_reset) {
reset();
}
return true;
}
return false;
}
private:
bool busy;
bool auto_reset;
std::chrono::time_point<std::chrono::system_clock> last_tick;
};
Usage
int main(){
timeout_trigger trigger;
while (trigger.is_over_limit(5)) {
break; // break after 5 seconds
}
return 0;
}