Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Timers and Alarms based on threads in modern C++

License

NotificationsYou must be signed in to change notification settings

hosseinmoein/Cheetah

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TimerAlarm Cheetah

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:

  1. It is templated with a class (functor) type. The functor (F) must define theopperator()().
  2. You instantiate an instance of TimerAlarm by calling the constructor and passing:
    1. A reference to an instance of the functor
    2. The second part of the time interval
    3. The nano-second part of the time interval. It is 0 by default
    4. The repeat count which specifies how many times the timer should repeat itself. It is set toFOREVER by default.
  3. You canarm() the TimerAlarm instance. That means the timer-alarm will now be in effect. Once armed one thread will be created to run the timer. The thread is never destroyed and reused repeatedly.
  4. You candisarm() the TimerAlarm instance. That means the timer will no longer execute. The thread is destroyed at this time but not before it is finished.
  5. You can always change the interval period by callingset_time_interval().
  6. You can query if the timer is armed by callingis_armed().
  7. You can query how many times the functor has been executed by callingcurrent_repeat_count().
  8. The destructor waits until all timer invocations are done.
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);}

About

Timers and Alarms based on threads in modern C++

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2026 Movatter.jp