C++ 中timer的使用

首先,要理解一下定时器。
定时器想做做的事情是在固定的时刻去做某件事情。
那么定时器要有几种功能

创建一个定时器,去让在固定时刻去做某种事情
停止这个定时器,当满足某种条件时,不用定时器的任务继续跑了,停止这个定时器的运行。
删除这个定时器,当某种情况满足的时候不需要这个定时器的功能了,结束此定时器。
在git hab上有一个专用的c++定时器
https://github.com/eglimi/cpptime

其中定时器的使用有oneshot和周期性的

A one shot timer.

using namespace std::chrono;
CppTime::Timer t;
t.add(seconds(2), { std::cout << “yes\n”; });
std::this_thread::sleep_for(seconds(3));
A periodic timer that is first executed after 2 seconds, and after this every second. The timeout event is then removed after 10 seconds. When a timeout event is removed, its attached handler is also freed to clean-up any attached resources.

using namespace std::chrono;
CppTime::Timer t;
auto id = t.add(seconds(2), { std::cout << “yes\n”; }, seconds(1));
std::this_thread::sleep_for(seconds(10));

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值