Run a function every x seconds in C ++

I am trying to create a channel reader in C ++, so I need a program to check for new feeds intermittently. However, the user still needs to be able to interact with the program, so the suggestion that I seem to keep looking for so that the system waits does not work for me. Can someone suggest a better solution, say, a timer that runs in the background or something like that?

Thanks Charles

+3
source share
6 answers

You can create a thread that will sleep for a certain period of time. It is an independent OS. Or, if you program in windows, you can configure a timer to send a timeout event periodically. The use of timers depends on your deployment platform.

+2
source

You will need threads. You have one thread in the background that runs a timer, and one in the foreground that interacts with the user. Then you have a shared memory area between the threads, which the background thread can change by calling your specific function; and that to the fore can be viewed. Perhaps look into the library of accelerating streams: http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html

+2
source

. -, , .

+1

SIGALRM, n . . .

void sigtime(int signo)
{
    signal(SIGALRM, sigtime);
}
....
signal(SIGALRM, sigtime);
itimerval itm;
itm.it_interval.tv_sec=0;
itm.it_value.tv_sec = 0;
itm.it_interval.tv_usec = 200000;
itm.it_value.tv_usec = 200000;
setitimer(ITIMER_REAL,&itm,0);

, , , - unix-like

+1

I also wanted to create a program that executes a function every x minutes or hours, and I found many examples, but to create it it was necessary to turn on and load the library, and for me it was inconvenient, and I made mine, not very logical)), but it works, you can see it below

 #include <stdlib.h>
 #include <iostream>
 #include <time.h>
 using namespace std;

 struct tm *addtime(struct tm *tm2)
 {
      time_t t = time(0);   // get time now
     struct tm * tm1  = localtime( & t );
     cout << " Time begin : " << tm1->tm_hour << " : " << tm1->tm_min <<endl;
     struct tm * aux = (struct tm*)malloc(sizeof (struct tm));

     aux->tm_sec = tm1->tm_sec + tm2->tm_sec;
     aux->tm_min = tm1->tm_min + tm2->tm_min + (aux->tm_sec / 60) ;
     aux->tm_hour = tm1->tm_hour + tm2->tm_hour +  (aux->tm_min / 60);
     aux->tm_min %= 60;
     aux->tm_sec %= 60;
     return (aux);

 }
 bool verif_time(struct tm *tm1)
 {

     time_t t = time(0);   // get time now
     struct tm * now = localtime( & t );
     if (tm1->tm_hour == now->tm_hour && tm1->tm_min == now->tm_min)
         return true;
     else
         return false;

 }

  int main()
  {
     struct tm * after = (struct tm*)malloc(sizeof (struct tm));

     after->tm_sec = 0; // here you can modify difference between now time and time when you want to execute function 
     after->tm_min = 1;
     after->tm_hour = 0;

     after = addtime(after);
     cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl;
     while (true)
     {
         if (verif_time(after))
         {
             cout << "Hello " << after->tm_hour << " : " << after->tm_min<<endl; //here you can include your function 

             after->tm_sec = 0;
             after->tm_min = 1;
             after->tm_hour = 0; // here also 

             after = addtime(after);
             cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl;


         }
     }

  }
0
source

Source: https://habr.com/ru/post/1745024/


All Articles