创建WallTimer的接口调用流程如图1所示。
与“定时器 > CreateTimer > 资源初始...”过程大致一样,必须先调用OpenHiva::Init接口进行初始化,此处不再赘述。
调用CreateWallTimer接口时,入参groupName必须和OpenHiva::Init接口中设置的groupName相同。此外,入参oneshot和autostart的配置,会影响WallTimer的可触发次数和启动方式。
定时器触发周期是触发回调函数的周期,当定时器时间到期后,会执行用户的回调函数。
进程结束前,调用OpenHiva::Shutdown接口进行资源清理。资源释放后,定义的OpenHiva接口将无法使用。
创建WallTimer的关键步骤代码示例如下,仅供参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#include <cstdlib>
#include <chrono>
#include <thread>
#include <string>
#include <stdlib.h>
#include <hiva.h>
#include "hiva_time/hiva_time.h"
#include "hiva_timer/wall_timer.h"
#include "open/init.h"
#include "open/node.h"
// 回调函数的子函数,用户自行定义
/static void PrintEvent(const Hiva::WallTimerEvent &evt)
{
std::cout << "\tlastExpected:" << evt.lastExpected << std::endl; // 上次回调应该发生的时刻
std::cout << "\tlastReal:" << evt.lastReal << std::endl; // 上次回调实际发生的时刻
std::cout << "\tlastExpired:" << evt.lastExpired << std::endl; // 上次定时器在timer server处过期,并向app侧发起通知的时刻
std::cout << "\tcurrentExpected:" << evt.currentExpected << std::endl; // 本次回调应该发生的时刻
std::cout << "\tcurrenReal:" << evt.currentReal << std::endl; // 本次回调实际发生的时刻
std::cout << "\tcurrenExpired:" << evt.currentExpired << std::endl; // 本次定时器在timer server处过期,并向app侧发起通知的时刻
}
/ 用户根据自身业务逻辑,定义回调函数,用于定时器到期后的处理
static void callback1(const Hiva::WallTimerEvent &evt)
{
HIVA_INFO("callback1 called:");
PrintEvent(evt);
}
int main(int argc, char **argv)
{
// 1. 资源初始化
bool oneshot = false; // 模式标识:一次性触发还是周期性触发
bool autostart = true; // 定时器自启动还是手动启动
int32_t sec = 1; // 定时器触发周期
int32_t sleepDuration = 60; // 模拟进程运行时间
if (argc == 1) {
HIVA_INFO("Default: oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
} elseif (argc == 5) {
oneshot = (bool)strtol(argv[1], NULL, 10);
autostart = (bool)strtol(argv[2], NULL, 10);
sec = (int32_t)strtol(argv[3], NULL, 10);
sleepDuration = (int32_t)strtol(argv[4], NULL, 10);
HIVA_INFO("oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
} else {
HIVA_ERROR("Input para is wrong, should be no input or 4 paras: oneshot, autostart, duration, process sleep time\n");
return -1;
}
// 定义线程组
std::vector<OpenHiva::ScheduleGroup> threadGroup;
OpenHiva::ScheduleGroup testGroup;
testGroup.groupName = "timer";
testGroup.scheduleType = OpenHiva::ScheduleType::UNBIND_AICPU;
threadGroup.push_back(testGroup);
// 调用资源初始化接口
OpenHiva::Init(argc, argv, threadGroup);
// 2. 创建Timer
// 构造Node对象
OpenHiva::Node node("timer_client_test");
HIVA_INFO("timer test: Client test start timer");
// 通过CreateWallTimer接口创建定时器,其中groupName要和Init接口里的ThreadGroup.groupName保持一致
Hiva::WallTimer t = Hiva::CreateWallTimer(Hiva::WallDuration(sec), callback1, "timer", oneshot, autostart);
HIVA_INFO("timer test: client test start to sleep");
std::this_thread::sleep_for(std::chrono::seconds(sleepDuration));
HIVA_INFO("timer test: finish sleep");
// 3. 资源释放
// 进程结束时,主动调用ShutDown函数释放资源
OpenHiva::Shutdown();
return 0;
}
|