以特定的时间间隔执行功能

m4n*_*n07 4 c pthreads timer

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

使用下面的代码,我能够Processfunction()每x秒调用一次。

问题:如何处理函数需要10秒钟以上才能完成执行的情况?

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


#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
}
Run Code Online (Sandbox Code Playgroud)

moo*_*eep 5

为什么需要一个计时器?

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

例:

#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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ 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
Run Code Online (Sandbox Code Playgroud)