Merge remote-tracking branch 'origin/timers' into refactor

dev-own-engine
marcelb 8 months ago
commit 8eff5ef1ea
  1. 14
      lib/rotor.hpp
  2. 28
      test/test.cpp

@ -22,6 +22,12 @@ int64_t rtime_ms() {
.count();
}
int64_t rtime_us() {
return chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
/**
* Intern class for timer async loop
*/
@ -38,7 +44,7 @@ class timer_core {
* Timer constructor, receives a callback function and time
*/
timer_core( function<void()> _callback, int64_t _time, bool _repeat):
callback(_callback), init(rtime_ms()), time(_time), repeat(_repeat), stop(false) {
callback(_callback), init(rtime_us()), time(_time*1000), repeat(_repeat), stop(false) {
}
/**
@ -83,7 +89,7 @@ class rotor {
else if (expired(tcores[i])) {
_asyncon.put_task(tcores[i]->callback);
if (tcores[i]->repeat) {
tcores[i]->init = rtime_ms();
tcores[i]->init = rtime_us();
}
else {
remove(i);
@ -91,7 +97,7 @@ class rotor {
}
}
}
this_thread::sleep_for(chrono::milliseconds(sampling));
this_thread::sleep_for(chrono::microseconds(sampling));
}
}
@ -99,7 +105,7 @@ class rotor {
* The method checks whether the time event has expired
*/
bool expired(shared_ptr<timer_core> tcore) {
return rtime_ms() - tcore->init >= tcore->time;
return rtime_us() - tcore->init >= tcore->time;
}
/**

@ -65,22 +65,26 @@ int main () {
* Init interval and timeout; clear interval and timeout
*/
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval 1: " << rtime_ms() - start << endl;
}, 50);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval 1: " << rtime_ms() - start << endl;
}, 100);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval 2: " << rtime_ms() - start << endl;
}, 200);
interval( [&] () {
cout << "interval 3: " << rtime_ms() - start << endl;
}, 300);
// interval( [&] () {
// cout << "interval " << rtime_ms() - start << endl;
// }, 200);
interval( [&] () {
cout << "interval 4: " << rtime_ms() - start << endl;
}, 400);
// interval inter1 ([&]() {
// cout << "interval prvi " << rtime_ms() - start << endl;

Loading…
Cancel
Save