项目作者: rklabs

项目描述 :
Asynchronous timer queue mechanism(C++11)
高级语言: C++
项目地址: git://github.com/rklabs/asynctimerqueue.git
创建时间: 2014-11-27T10:36:27Z
项目社区:https://github.com/rklabs/asynctimerqueue

开源协议:GNU General Public License v3.0

下载


AsyncTimer

Asynchronous timer queue mechanism(C++11)

Build Status

This is an implementation of asynchronous timer queue. Callbacks can be registered
to be run in future. Time has to be specified in millisec. An “event” can be created
to run once or repeatedly. AsyncTimerQueue class has been implemented as singleton.
The application intending to use AsyncTimerQueue must run Timer::AsyncTimerQueue::timerLoop
in a separate thread. Below is simple example.

Event handler signature should be as follows ‘void func(type1 arg1, type2, arg2, …)’

  1. #include "asynctimerqueue.hh"
  2. ...
  3. ...
  4. class foo {
  5. public:
  6. void handler3() {
  7. std::cout << "handler3" << std::endl;
  8. }
  9. };
  10. int main() {
  11. std::thread asyncthread(&Timer::AsyncTimerQueue::timerLoop,
  12. &Timer::AsyncTimerQueue::Instance());
  13. foo f;
  14. int eventId1 = Timer::AsyncTimerQueue::Instance().create(1000, true, &handler1);
  15. int eventId2 = Timer::AsyncTimerQueue::Instance().create(2000, true, &handler2);
  16. int eventId3 = Timer::AsyncTimerQueue::Instance().create(4000, true, &foo::handler3, &f);
  17. std::this_thread::sleep_for(std::chrono::seconds(2));
  18. Timer::AsyncTimerQueue::Instance().cancel(eventId1);
  19. asyncthread.join();
  20. }