Developing timer API in C++, under Linux. I want to called periodically task, How I can create timer using which i can called API for some specific task. I have already used Qtimer in qt. But need to implement using standard C library. Sorry for English....
-
"Peridially"? As in "closest to the day"? – Kerrek SB Mar 05 '15 at 09:24
-
2How time critical is this? You could use cron to run you program instead. – doctorlove Mar 05 '15 at 09:24
-
You need to speak English well enough to at least not sound incoherent. Sorry, but we need to understand you to help you. – sashoalm Mar 05 '15 at 09:24
-
Sorry for English...time not much critical.. – Dipak D Desai Mar 05 '15 at 09:30
2 Answers
This question has many possible answers.
- If you are used to Qt, use Qt.
- If you want a C answer, you could
sleep()
- see this question, though you have tagged your question C++ rather than C. - If you want a C or C++ answer you could use threads.
- You could write your task as a program and get something else to run it in a schedule.
You don't say how often. If you just need something to happen daily or hourly let the OS do the work and use cron
If you need something to kick off every second precisely, you are in trouble. Unless you can have a dedicated machine with nothing else interrupting your process.
And what happens if the last chunk of work hasn't completed when you want to start the next time?
Does anything need to know the results of this, or is it just sending a mail shot to whoever is listening? Do you need to know if the task complete or even began ok? If your program is trying to do something every five minutes, what will you do if it crashes? Or the machine it runs on catches fire?

- 1
- 1

- 18,872
- 2
- 46
- 62
-
Thank I just want to used C++ standard library for this. I want to create timer as event based. I like option to used thread. I want to generate event every 15 minute. – Dipak D Desai Mar 05 '15 at 09:45
-
There's no Timer in C++ - some platforms provide one for you, but you haven't given enough details about your set up. If you can use C++111 there are some other questions that show you how to do this e.g. http://stackoverflow.com/questions/14650885/how-to-create-timer-events-using-c-11 – doctorlove Mar 05 '15 at 09:50
Use the Linux standard C library's setitimer( )
.
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
void periodic_task (int signum)
{
static int count = 0;
printf (" periodic task in C++ timer %d \n", ++count);
}
int main ()
{
struct sigaction sa;
struct itimerval timer;
/* Install periodic_task as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &periodic_task ;
sigaction (SIGVTALRM, &sa, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 250000;
/* Start a virtual timer. It counts down whenever this process is executing. */
setitimer (ITIMER_VIRTUAL, &timer, NULL);
/* Do busy work. */
while (1);
}

- 8,490
- 3
- 24
- 28
-
-
There is a good example here : http://stackoverflow.com/questions/2086126/need-programs-that-illustrate-use-of-settimer-and-alarm-functions-in-gnu-c – Software_Designer Mar 09 '15 at 12:15