Fix timer

This commit is contained in:
hodasemi 2019-01-05 12:45:02 +01:00
parent 6c2aaf897a
commit ed188fd21c

View file

@ -7,14 +7,15 @@
typedef std::chrono::duration<int, std::milli> milliseconds_type; typedef std::chrono::duration<int, std::milli> milliseconds_type;
class Counter { class Counter
private: {
private:
int max; int max;
int n; int n;
std::chrono::_V2::system_clock::time_point last_time; std::chrono::system_clock::time_point last_time;
milliseconds_type update_interval; milliseconds_type update_interval;
public: public:
Counter(int max, float interval) Counter(int max, float interval)
: max(max), update_interval((int)(interval * 1000.0f)) : max(max), update_interval((int)(interval * 1000.0f))
{ {
@ -24,16 +25,19 @@ public:
~Counter() {} ~Counter() {}
int get_counter() { int get_counter()
{
auto new_time = std::chrono::high_resolution_clock::now(); auto new_time = std::chrono::high_resolution_clock::now();
milliseconds_type time_difference = std::chrono::duration_cast<milliseconds_type>(new_time - last_time); milliseconds_type time_difference = std::chrono::duration_cast<milliseconds_type>(new_time - last_time);
if (time_difference.count() >= update_interval.count()) { if (time_difference.count() >= update_interval.count())
{
last_time += update_interval; last_time += update_interval;
n++; n++;
if (n >= max) { if (n >= max)
{
n = n - max; n = n - max;
abort(); abort();
} }