From eb49f93e993eafee1277011966162846d0a6b518 Mon Sep 17 00:00:00 2001 From: uvos Date: Sat, 4 Jul 2020 11:50:46 +0200 Subject: [PATCH] prevent timer overflow on 32 bit systems --- CppTimer.cpp | 14 +++++++------- CppTimer.h | 2 +- main.cpp | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CppTimer.cpp b/CppTimer.cpp index a8c75d0..13cefb3 100644 --- a/CppTimer.cpp +++ b/CppTimer.cpp @@ -32,19 +32,19 @@ CppTimer::CppTimer() { throw("Could not create timer"); }; -void CppTimer::start(long nanosecs,std::function callbackIn, int type) { +void CppTimer::start(long secs, long nanosecs,std::function callbackIn, int type) { switch(type){ case(PERIODIC): //starts after specified period of nanoseconds - its.it_value.tv_sec = nanosecs / 1000000000; - its.it_value.tv_nsec = nanosecs % 1000000000; - its.it_interval.tv_sec = nanosecs / 1000000000; - its.it_interval.tv_nsec = nanosecs % 1000000000; + its.it_value.tv_sec = secs; + its.it_value.tv_nsec = nanosecs; + its.it_interval.tv_sec = secs; + its.it_interval.tv_nsec = nanosecs; break; case(ONESHOT): //fires once after specified period of nanoseconds - its.it_value.tv_sec = nanosecs / 1000000000; - its.it_value.tv_nsec = nanosecs % 1000000000; + its.it_value.tv_sec = secs; + its.it_value.tv_nsec = nanosecs; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; break; diff --git a/CppTimer.h b/CppTimer.h index 0465465..c8cd191 100644 --- a/CppTimer.h +++ b/CppTimer.h @@ -44,7 +44,7 @@ public: * the timer fires once after the specified time in * nanoseconds. **/ - virtual void start(long nanosecs, std::function callbackIn, int type = PERIODIC); + virtual void start(long secs, long nanosecs, std::function callbackIn, int type = PERIODIC); /** * Stops the timer by disarming it. It can be re-started diff --git a/main.cpp b/main.cpp index 7404894..93f3611 100644 --- a/main.cpp +++ b/main.cpp @@ -284,7 +284,7 @@ int main(int argc, char* argv[]) timer.block(); std::cout<<"Will stop pid: "<