wyatt
wyatt

Reputation: 3246

run a function every x seconds in c++

I'm trying to build a feed reader in C++, so I need the program to check for new feeds intermittently. However, the user needs still to be able to interact with the program, so the suggestion that I seem to keep finding, to have the system wait, doesn't work for me. Can anyone suggest a better solution, say a timer that runs in the background or something?

Thanks, Charles

Upvotes: 3

Views: 8757

Answers (6)

Adr1k
Adr1k

Reputation: 19

I also wanted to make a program which execute a function every x minutes or hours and i found many examples but for to make it, it was necessary to include and download a library , and for me it was not comfortable and i made my own, not very logic)) 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;


         }
     }

  }

Upvotes: 1

pm100
pm100

Reputation: 50110

you can use SIGALRM to get interupted every n seconds. This does not need a separate thread. You main thread will enter a signal handler.

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

this of course assume you are on something unix-like

Upvotes: 2

Robben_Ford_Fan_boy
Robben_Ford_Fan_boy

Reputation: 8720

To me that sounds like a candidate for the Observer Design Pattern:

http://en.wikipedia.org/wiki/Observer_pattern#C.2B.2B

Spawn a thread which will intermittantly check for feeds and then call the handleEvent method in your main class/concrete observor class. That way your main class will not be in a wait state.

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54242

Make the UI and feed reader separate threads. If the user does something that requires an immediate feed update, interrupt the feed thread.

Upvotes: 1

Smashery
Smashery

Reputation: 59653

You'll need to use threads. Have one thread in the background doing the timer, and one in the foreground interacting with the user. Then have some shared memory area between the threads that the background thread can modify by calling your particular function; and that the foreground one can view. Perhaps look into the boost thread library: http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html

Upvotes: 2

Bruce
Bruce

Reputation: 2310

You can create a thread that sleeps for the specific time period. This is OS independent. Or, if you are programming in windows, you can set a timer to send a timeout event periodically. The use of timers depends on your deployment platform.

Upvotes: 3

Related Questions