项目作者: walker-zheng

项目描述 :
easy timer
高级语言: C++
项目地址: git://github.com/walker-zheng/timer.git
创建时间: 2017-10-27T06:54:10Z
项目社区:https://github.com/walker-zheng/timer

开源协议:MIT License

下载


easy timer

  1. 功能
  • priority_queue实现简单定时器
  • 支持 std::function 添加,根据id删除
  • 支持 时间点、循环、指定次数等,可嵌套组合使用
  1. 使用示例:

    1. easy::Timer timer;
    2. bar b;
    3. std::vector<int> taskids;
    4. auto curr = system_clock::now();
    5. std::cout << "> timer " << ": " << (duration<double>(curr - now)).count() << "s => Start!" << std::endl;
    6. taskids.push_back(timer.add(foo, curr + seconds(2)));
    7. taskids.push_back(timer.add(std::bind(&bar::hello, b), curr + seconds(3), seconds(3), 3));
    8. taskids.push_back(timer.add(
    9. [ = ]() { std::cout << (duration<double>(system_clock::now() - now)).count() << "s => func lambda" << std::endl; },
    10. curr + seconds(4), seconds(2)));
    11. std::this_thread::sleep_for(seconds(10));
    12. for (auto id : taskids)
    13. {
    14. std::cout << "> timer " << ": del " << id << std::endl;
    15. timer.del(id);
    16. }
    17. timer.stop();
    18. curr = system_clock::now();
    19. std::cout << "> timer " << ": " << (duration<double>(curr - now)).count() << "s => Stop!" << std::endl;
  2. timer嵌套 每天一个定时器做当天任务:

    1. easy::Timer g_timer;
    2. g_timer.add([ = ]()
    3. {
    4. easy::Timer timer;
    5. bar b;
    6. std::vector<int> taskids;
    7. auto curr = system_clock::now();
    8. std::cout << "> timer " << ": " << (duration<double>(curr - now)).count() << "s => Start!" << std::endl;
    9. taskids.push_back(timer.add(foo, curr + seconds(2)));
    10. taskids.push_back(timer.add(std::bind(&bar::hello, b), curr + seconds(3), seconds(3), 3));
    11. taskids.push_back(timer.add(
    12. [ = ]() { std::cout << (duration<double>(system_clock::now() - now)).count() << "s => func lambda" << std::endl; },
    13. curr + seconds(4), seconds(2)));
    14. std::this_thread::sleep_for(seconds(10));
    15. for (auto id : taskids)
    16. {
    17. std::cout << "> timer " << ": del " << id << std::endl;
    18. timer.del(id);
    19. }
    20. timer.stop();
    21. curr = system_clock::now();
    22. std::cout << "> timer " << ": " << (duration<double>(curr - now)).count() << "s => Stop!" << std::endl;
    23. },
    24. system_clock::now() + seconds(4), seconds(15));
    25. std::this_thread::sleep_for(seconds(60));