本文主要是介绍c++ 定时器(多媒体定时器, posix定时器),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
项目地址
- CMAKE 管理项目
- windows使用的是: 多媒体定时器
- linux 使用的是: posix timer
- 地址: https://gitee.com/mohistH/timer-pp
- 需要c++11及以上支持
使用范例
接口使用顺序
- 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中获取
这篇关于c++ 定时器(多媒体定时器, posix定时器)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!