Execute a method every x seconds in C
问题 Is there an example of a working timer that executes some function every x amount seconds using C. I'd appreciate an example working code. 回答1: You could spawn a new thread: void *threadproc(void *arg) { while(!done) { sleep(delay_in_seconds); call_function(); } return 0; } ... pthread_t tid; pthread_create(&tid, NULL, &threadproc, NULL); Or, you could set an alarm with alarm(2) or setitimer(2): void on_alarm(int signum) { call_function(); if(!done) alarm(delay_in_seconds); // Reschedule