Implementing a timer
Let us implement a timer that accepts intervals and callback functions. The timer will execute the callback function at each interval. Also, the user will be able to stop the timer by calling its stop()
function.
The following snippet shows an implementation of the timer:
#include <chrono> #include <functional> #include <iostream> #include <syncstream> #include <thread> #define sync_cout std::osyncstream(std::cout) using namespace std::chrono_literals; using namespace std::chrono; template<typename Duration> class Timer { public: typedef std::function<void(void)> Callback; Timer(const Duration interval, const Callback& callback) { auto value = duration_cast<milliseconds>(interval); sync_cout...