以特定时间间隔执行函数

编程入门 行业动态 更新时间:2024-10-27 08:38:04
本文介绍了以特定时间间隔执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任务是每 x(比如 x=10)秒执行一个函数(比如 Processfunction()).

The task is to execute a function (say Processfunction()) every x (say x=10) seconds.

使用下面的代码,我可以每 x 秒调用一次 Processfunction().

With below code, I'm able to call Processfunction() every x seconds.

问题:函数执行时间超过10秒的情况如何处理?

Question: How to handle the case where the function takes more than 10 seconds to finish execution?

一种方法是使用一个标志来指示 Processfunction() 执行的结束并在调用 Processfunction() 之前检查它.有一个更好的方法吗 ?

One way would be to have a flag to indicate the end of Processfunction() execution and check for it before calling Processfunction(). Is there a better way to do this ?

#include <pthread.h> #include <unistd.h> // for sleep() and usleep() void *timerthread(void *timer_parms) { struct itimerspec new_value; int max_exp, fd; struct timespec now; uint64_t exp; ssize_t s; struct timer_params *p =(struct timer_params*)timer_parms; printf("starttimer Start\n"); /* Create a CLOCK_REALTIME absolute timer with initial expiration and interval as specified in command line */ if (clock_gettime(CLOCK_REALTIME, &now) == -1) handle_error("clock_gettime"); new_value.it_value.tv_sec = now.tv_sec; new_value.it_value.tv_nsec = now.tv_nsec + p->tv_nsec; new_value.it_interval.tv_sec = p->tv_sec; new_value.it_interval.tv_nsec = p->tv_nsec; //max_exp = 5; //No of times fd = timerfd_create( CLOCK_REALTIME , 0); if (fd == -1) handle_error("timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1) handle_error("timerfd_settime"); printf("timer started\n"); while(1) // keep checking { s = read(fd, &exp, sizeof(uint64_t)); if (s != sizeof(uint64_t)) handle_error("read"); Processfunction(); // Say after X seconds call this function } return NULL; } int main() { struct timer_params timer_params_obj; int res;void *thread_result; timer_params_obj.tv_sec = 10; //timer_params_obj.tv_nsec = 10000000 ; //10ms timer_params_obj.tv_nsec = 0 ; pthread_t pt; pthread_create(&pt, NULL, timerthread, &timer_params_obj); // thread is running and will call Processfunction() every 10 sec }

推荐答案

为什么需要计时器?

您可以只测量执行时间并根据经过时间与所需间隔持续时间的关系进行睡眠.

You could just measure the execution time and take a sleep according to the relation of elapsed time to desired interval duration.

示例:

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> int main() { srand(1); for (;;) { double interval = 10; /* seconds */ /* start time */ time_t start = time(NULL); /* do something */ int duration = rand() % 13; printf("%2d seconds of work started at %s", duration, ctime(&start)); sleep(duration); /* end time */ time_t end = time(NULL); /* compute remaining time to sleep and sleep */ double elapsed = difftime(end, start); int seconds_to_sleep = (int)(interval - elapsed); if (seconds_to_sleep > 0) { /* don't sleep if we're already late */ sleep(seconds_to_sleep); } } return 0; }

输出:

$ gcc test.c && ./a.out 0 seconds of work started at Sun Mar 17 21:20:28 2013 9 seconds of work started at Sun Mar 17 21:20:38 2013 11 seconds of work started at Sun Mar 17 21:20:48 2013 4 seconds of work started at Sun Mar 17 21:20:59 2013 1 seconds of work started at Sun Mar 17 21:21:09 2013 ^C

更多推荐

以特定时间间隔执行函数

本文发布于:2023-10-16 11:26:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   函数   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!