tags:

views:

131

answers:

5

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

+1  A: 

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.

Bruce
Thanks, I'll have a look at that. I know it's quite subjective, but does it seem a little messy to simply have a thread pause, or is this just the way it's done?
wyatt
Thread pauses are in fact really good - the operating system can say to itself "Okay, he won't be using that for the next x seconds; I can schedule some other task in the meantime". The alternative would be to have some loop which is running constantly and is constantly checking "is the time up yet?" In that case, the Operating System will continue to allocate time to the "paused" thread; which is thus more inefficient.
Smashery
Be aware if you just have fixed pauses of 1 second, and the work takes half a second to complete, you end up running every 1.5 seconds. You might want to take in to account the time the work took to do.
AshleysBrain
+2  A: 

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

Smashery
+1  A: 

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

Brendan Long
A: 

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.

David Relihan
+1  A: 

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

pm100
+1 and http://www.opengroup.org/onlinepubs/009695399/functions/getitimer.html see also alarm http://www.opengroup.org/onlinepubs/009695399/functions/alarm.html which forgoes the structure for simple integral seconds
Potatoswatter