编程语言
首页 > 编程语言> > c++ 定时器(多媒体定时器, posix定时器)

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

作者:互联网

项目地址

使用范例

接口使用顺序

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

标签:定时器,virtual,ITimerPP,timer,int,posix,c++,public
来源: https://www.cnblogs.com/pandamohist/p/16398523.html