Fix timer
This commit is contained in:
parent
6c2aaf897a
commit
ed188fd21c
1 changed files with 12 additions and 8 deletions
|
@ -7,16 +7,17 @@
|
||||||
|
|
||||||
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))
|
||||||
{
|
{
|
||||||
last_time = std::chrono::high_resolution_clock::now();
|
last_time = std::chrono::high_resolution_clock::now();
|
||||||
n = 0;
|
n = 0;
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue