我需要能够同时启动多个计时器,并明确知道计时器是否已停止或仍在运行。
#define RESEND_TIMEOUT 5
void timer_set_timeout(timer_t * timer, uint32_t timeout);
void timer_start(timer_t * timer);
bool timer_complete(timer_t * timer);
void signal_handler(int sig, siginfo_t *si, void *uc);
int main() {
timer_t resend_timer;
printf("starting timer\n");
timer_start(&resend_timer);
timer_set_timeout(RESEND_TIMEOUT);
while(1) {
if (timer_complete(&resend_timer))
break;
}
}
void timer_set_timeout(timer_t * timer, uint32_t timeout)
{
struct itimerspec it_val;
it_val.it_value.tv_sec = timeout;
it_val.it_value.tv_nsec = 0;
it_val.it_interval.tv_sec = 0;
it_val.it_interval.tv_nsec = 0;
if (timer_settime(*timer, 0, &it_val, NULL) == -1) {
errExit("Could not set timeout");
}
}
void timer_start(timer_t * timer)
{
struct sigaction sa;
struct sigevent sev;
// establish signal handler
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = signal_handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG, &sa, NULL) == -1)
errExit("Failed to establish signal handler");
// create timer
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = timer;
if(timer_create(CLOCK_MONOTONIC, &sev, timer) == -1) {
errExit("Could not start timer");
}
}
// return true if timer ended
bool timer_complete(timer_t * timer)
{
if(timer_getoverrun(*timer) == 0)
return false;
else
return true;
}
void signal_handler(int sig, siginfo_t *si, void *uc)
{
printf("Timer ran out!\n");
signal(sig, SIG_IGN);
}
信号处理程序在 5 秒后运行,但我无法知道计时器是否已结束或未使用 timer_complete。我尝试过: int timer_gettime(timer_t timerid, struct itimerspec *curr_value) 但我不知道为 curr_value 传递什么。我尝试声明一个itimerspec,但它给出的结果与getoverrun 相同。
我还尝试使用timer_settime 的标志TIMER_ABSTIME,但由于某种原因,计时器立即到期。