BTimer¶
定义在头文件 <BUtils/BTimer> 中
概述¶
Class BTimer provides repetitive and single-shot timers with a minimum precision of 1 millisecond.
公有类型¶
enum | BTimerStatus {Active, Stop} |
属性¶
bool | singleShot |
std::chrono::milliseconds | interval |
std::chrono::milliseconds | timeout |
公有函数¶
BTimer() |
|
~BTimer() |
|
bool | operator>(const BTimer& rtimer) const |
bool | operator<(const BTimer& rtimer) const |
bool | operator=(const BTimer& rtimer) const |
void | start() |
void | stop() |
bool | isActive() const |
bool | isSingleShot() const |
int32 | id() const |
uint32 | interval() const |
uint32 | timeout() const |
void | reset() |
void | setActive(bool) |
void | callOnInterval(std::function timer_action) |
void | callOnTimeout(std::function timer_action) |
void | setInterval(uint32 _interval) |
void | setInterval(std::chrono::milliseconds _interval) |
void | setTimeout(uint32 _timeout) |
void | setTimeout(std::chrono::milliseconds _timeout) |
void | setSingleShot(bool singleshot) |
静态公有函数¶
uint | precision() |
void | setPrecision(uint) |
详细描述¶
Class BTimer provides repetitive and single-shot timers with a minimum precision of 1 millisecond.
BTimer 提供一种简单易用的编程界面,用于在您的应用中执行周期性任务。只需要创建一个 定时器 对象,并设置相应的属性,然后启动它。你可以在任何时候改变定时器的属性值。
1 秒的定时器例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <BUtils/BTimer>
#include <unistd.h>
#include <iostream>
void timerAction() {
std::cout << "I'm timer action." << std::endl;
}
int main() {
BUtils::BTimer timer;
timer.callOnTimeout(timerAction);
timer.setTimeout(1000);
timer.start();
// If timer object is destroyed, the timer event do not exist as well.
// Sleep to make the timeout event occur.
sleep(2);
}
|
BTimer 的定时器事件系统被设计为可以在多线程环境下工作,但是 BTimer 对象本身并不能做到这一点。不要尝试在线程间共享 BTimer 对象,就在一个线程中创建并使用即可。
精确度和定时器精度¶
定时器的精确度取决于底层操作系统和硬件设备。在大多数系统平台上,系统时钟普遍可以达到 1 微秒的精确度。
大多数系统平台上,BTimer 支持 1 毫秒的最低精度。但是在定时器系统工作负载非常高(例如非常多的定时器事件)或者高 CPU 使用率(这将导致定时器事件循环不能立刻被操作系统唤醒)的情况下或导致精度不准(一个定时器循环时间大于 1 毫秒)。
成员类型文档¶
-
enum
BTimerStatus
¶
这个枚举类型被这些函数使用: isActive() const
and setActive(bool)
.
常量 | 值 | 描述 |
---|---|---|
BTimer::Active | 0 | 定时器被激活 |
BTimer::Stop | 1 | 定时器停止 |
属性文档¶
-
bool
singleShot
¶
这个属性值表示是否要在间隔超时发生的时候触发间隔动作。
如果为真,间隔动作会在每个间隔周期后被触发,除非定时器超时。默认值为假。
访问函数:¶
bool | isSingleShot() const |
void | setSingleShot(bool singleshot) |
-
std::chrono::milliseconds
interval
¶
这个属性值表示该定时器的间隔周期。在每个间隔周期后,间隔动作会被触发。默认值是 0,代表没有间隔。
访问函数:¶
uint32 | interval() const |
void | setInterval(std::chrono::milliseconds) |
-
std::chrono::milliseconds
timeout
¶
这个属性值表示该定时器的过期时间(超时时间)。超时之后,该定时器会从定时器系统中移除直到下一个 start 调用。
注解
默认值是最大的无符号整型,代表该定时器”永不超时”。
访问函数:¶
uint32 | timeout() const |
void | setTimeout(std::chrono::milliseconds) |
成员函数文档¶
构造一个 BTimer 对象。
-
BTimer
::
~BTimer
()¶
析构一个 BTimer 对象。
-
bool
BTimer
::
operator>
(const BTimer &rtimer) const¶
如果 id 大于 rtimer’s id 则返回真。
-
bool
BTimer
::
operator<
(const BTimer &rtimer) const¶
如果 id 小于 rtimer’s id 则返回真。
-
bool
BTimer
::
operator==
(const BTimer &rtimer) const¶
如果 id 等于 rtimer’s id 则返回真。
-
void
BTimer
::
start
()¶
开启该定时器;如果超时时间设置为 0 则该函数不做任何事情。
-
void
BTimer
::
stop
()¶
停止该定时器;如果定时器已经过期则该函数不做任何事情。
-
bool
BTimer
::
isActive
() const¶
如果定时器正在运行则返回真。
-
bool
BTimer
::
isSingleShot
() const¶
如果间隔事件只触发一次则返回真。
-
int32
BTimer
::
id
() const¶
返回定时器的 id。
-
uint32
BTimer
::
interval
() const¶
返回定时器超时间隔周期,以毫秒为单位。
-
uint32
BTimer
::
timeout
() const¶
返回定时器超时时间,以毫秒为单位。
-
void
BTimer
::
reset
()¶
重置该定时器的所有属性值(除了定时器 id)为默认值并停止该定时器。
-
void
BTimer
::
setActive
(bool _active)¶
用户调用则该函数不做任何事情。
-
void
BTimer
::
callOnInterval
(std::function<void()> timer_action)¶
设置超时间隔之后要触发的动作。
-
void
BTimer
::
callOnTimeout
(std::function<void()> timer_action)¶
设置超时之后要触发的动作。
-
void
BTimer
::
setInterval
(uint32 _interval)¶
-
void
BTimer
::
setInterval
(std::chrono::milliseconds _interval)¶
设置超时间隔,以毫秒为单位。默认值是 0。
-
void
BTimer
::
setTimeout
(uint32 _timeout)¶
-
void
BTimer
::
setTimeout
(std::chrono::milliseconds _timeout)¶
设置超时时间,以毫秒为单位。默认值是最大的无符号整型。
-
void
BTimer
::
setSingleShot
(bool singleshot)¶
如果 singleshot 为真,则间隔动作只会被触发一次。
-
static uint
BTimer
::
precision
()¶
返回定时器的精度,以毫秒为单位。默认值是 1 毫秒。
-
static void
BTimer
::
setPrecision
(uint)¶
设置定时器的精度,以毫秒为单位。