c++ 定时器(多媒体定时器, posix定时器)

项目地址

使用范例

接口使用顺序

  • 1 调用 init_
  • 2 启动定时器 begin_
  • 3 停止定时器 end_

ITimerPP类接口

class ITimerPP
{

public:

	using uint = unsigned int;

	enum TimerType
	{
		/// 单次执行
		TIMER_TYPE_ONE_SHORT	= 0,
		/// 周期执行
		TIMER_TYPE_PERIODIC		= 1,
	};

public:
	virtual ~ITimerPP() {  }

	virtual int init_(const TimerType&& tt, ITimerCallBack* pcb) = 0;

	virtual int begin_(const uint&& interval_ms) = 0;

	virtual int end_() = 0;
};

实现超时处理函数即可

class demoTimerCallback : public oct_tk::ITimerCallBack
{
public:
    virtual void timer_call_back_()
    {
        static int index = 0; 
        std::cout << "\n index=" << ++index << "=";
    }
};

创建定时器

demoTimerCallback tc;

    std::unique_ptr<ITimerPP> demo_timer = oct_tk::new_itimer_();
    ITimerPP* ptimer = demo_timer.get();

    ptimer->init_(oct_tk::ITimerPP::TIMER_TYPE_PERIODIC, &tc);
    ptimer->begin_(40);

    std::this_thread::sleep_for(std::chrono::seconds(1 * 30));
    ptimer->end_();

NOTE

  • 完整范例可在项目中 example中 main.cc中获取

更新日志

  • 2023-03-11
    • 重新封装windows多媒体定时器。 完整代码如下(copy即可使用)

TimerImp.h

#ifndef TIMER_IMP_H_
#define TIMER_IMP_H_
#include <functional>
#include <string>
#include <atomic>

namespace oct_tm
{
	class NonCopyable
	{
	protected:
		NonCopyable() {}
		virtual
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值