Need help in implementing a ticker class with start/stop/pause functionality, assignable callback (onTick) with execution on a separate thread every interval span. Interval span is specifiable and update-able. Hopefully it should be cross-platform.
Here is my naive attempt, but it's not good (the while loop in start() is blocking currently, but ideally it should run on a separate thread but I can't figure out how to achieve it) as I'm pretty noob in C++ multithreading model:
#include <cstdint>
#include <functional>
#include <chrono>
#include <thread>
#include <future>
class Ticker {
public:
typedef std::chrono::duration<int64_t, std::nano> tick_interval_t;
typedef std::function<void()> on_tick_t;
Ticker (std::function<void()> onTick, std::chrono::duration<int64_t, std::nano> tickInterval)
: _onTick (onTick)
, _tickInterval (tickInterval)
, _running (false) {}
~Ticker () {}
void start () {
if (_running) return;
_running = true;
while (_running) {
std::async( std::launch::async, _onTick );
std::this_thread::sleep_for( _tickInterval );
}
}
void stop () { _running = false; }
private:
on_tick_t _onTick;
tick_interval_t _tickInterval;
bool _running;
};
I'm a completely wrong in my attempt or it's pretty close?
Consider the following code with example of usage. I hope I understood you correctly. Just run your
while
loop in a separate thread.