This is a light-weight, header-only C++ Timer-alarm library that's based on threads as opposed to the POSIX' signal based timers. Therefore, it is a lot less disruptive.
It has the following logic:
classMyFoot {public:MyFoot (int id) : id_ (id), count_ (0) { }booloperator ()() { std::cout <<"Printing from MyFoot (" << id_ <<"). count is" << count_ <<". time is" <<time(nullptr) << std::endl; count_ +=1;return (true); }private:int id_;size_t count_;};// ----------------------------------------------------------------------------intmain(int,char *[]) {conststruct ::timespec rqt = {60,0 }; { MyFootfoot_master (10);// The functor foot_master must be executed every 5 seconds// "interval_nanosec" is set to zero by default.// "repeat_count" is set to forever by default// TimerAlarm<MyFoot>timer (foot_master,5);// The Timer will be armed// timer.arm();conststruct ::timespec rqt2 = {30,0 };nanosleep(&rqt2,nullptr);// Change the interval to 1 seconds.// "interval_nanosec" is set to zero by default.// timer.set_time_interval(1);nanosleep(&rqt2,nullptr);// Change the interval to 10 seconds.// "interval_nanosec" is set to zero by default.// timer.set_time_interval(10);nanosleep(&rqt,nullptr); MyFootfoot_master2 (200);// Construct a second timer with specifed parameters.// TimerAlarm<MyFoot>timer2 (foot_master2,// Functor instance5,// 5 seconds intervals0,// 0 nano-seconds specified TimerAlarm<MyFoot>::FOREVER);// Repeat forever timer2.arm();// Armed timer will executenanosleep(&rqt,nullptr); } std::cout <<"\n\nmain(): Got out of the enclosing block ...\n" << std::endl; MyFootfoot_master (3000);// Construct a third timer with specifed parameters.// TimerAlarm<MyFoot>timer (foot_master,// Functor instance5,// 5 seconds intervals0,// 0 nano-seconds specified5);// Repeat 5 times timer.arm();// Armed timer will executenanosleep(&rqt,nullptr);return (EXIT_SUCCESS);}