-4

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....

4
  • "Peridially"? As in "closest to the day"?
    – Kerrek SB
    Mar 5, 2015 at 9:24
  • 2
    How time critical is this? You could use cron to run you program instead.
    – doctorlove
    Mar 5, 2015 at 9: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 5, 2015 at 9:24
  • Sorry for English...time not much critical.. Mar 5, 2015 at 9:30

2 Answers 2

0

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);

}
2
0

This question has many possible answers.

  1. If you are used to Qt, use Qt.
  2. If you want a C answer, you could sleep() - see this question, though you have tagged your question C++ rather than C.
  3. If you want a C or C++ answer you could use threads.
  4. 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?

2
  • 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. Mar 5, 2015 at 9: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. stackoverflow.com/questions/14650885/…
    – doctorlove
    Mar 5, 2015 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.