GitHub - hosseinmoein/Cheetah: Timers and Alarms based on threads in modern C++ (original) (raw)
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:
class MyFoot { public:
MyFoot (int id) : id_ (id), count_ (0) { }
bool operator ()() {
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_;};
// ----------------------------------------------------------------------------
int main(int, char *[]) {
const struct ::timespec rqt = { 60, 0 };
{
MyFoot foot_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();
const struct ::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);
MyFoot foot_master2 (200);
// Construct a second timer with specifed parameters.
//
TimerAlarm<MyFoot> timer2 (foot_master2, // Functor instance
5, // 5 seconds intervals
0, // 0 nano-seconds specified
TimerAlarm<MyFoot>::FOREVER); // Repeat forever
timer2.arm(); // Armed timer will execute
nanosleep(&rqt, nullptr);
}
std::cout << "\n\nmain(): Got out of the enclosing block ...\n" << std::endl;
MyFoot foot_master (3000);
// Construct a third timer with specifed parameters.
//
TimerAlarm<MyFoot> timer (foot_master, // Functor instance
5, // 5 seconds intervals
0, // 0 nano-seconds specified
5); // Repeat 5 times
timer.arm(); // Armed timer will execute
nanosleep(&rqt, nullptr);
return (EXIT_SUCCESS);}