perform operation at a certain interval
Say I want to perform the action at a certain timeout, like fire an
event. I figured out how to do every n number of seconds, but not 1.5
seconds. Here is what I have.Please suggest how to handle my case:
extern int PUBLISH_TIMEOUT;
void Publish()
{
static int local_time=time(NULL);
int current_time = time (NULL);
if((current_time+PUBLISH_TIME)>local_time)
{
fireEvent();
local_time=current_time;
}
}
Re: perform operation at a certain interval
puzzlecracker <ironsel2000@gmail.com> writes:[color=blue]
>Say I want to perform the action at a certain timeout, like fire an
>event. I figured out how to do every n number of seconds, but not 1.5
>seconds. Here is what I have.Please suggest how to handle my case:
>
>extern int PUBLISH_TIMEOUT;
>
>void Publish()
>{
> static int local_time=time(NULL);
>
> int current_time = time (NULL);
> if((current_time+PUBLISH_TIME)>local_time)
> {
> fireEvent();
> local_time=current_time;
> }
>}[/color]
Read the man page for the time(2) system call:
$ man 2 time
Then, follow the SEE ALSO section and examine in
turn each of the references.
scott
Re: perform operation at a certain interval
On Sep 19, 5:03 am, puzzlecracker <ironsel2...@gmail.com> wrote:[color=blue]
> Say I want to perform the action at a certain timeout, like fire an
> event. I figured out how to do every n number of seconds, but not 1.5
> seconds. Here is what I have.Please suggest how to handle my case:
>
> extern int PUBLISH_TIMEOUT;
>
> void Publish()
> {
> static int local_time=time(NULL);
>
> int current_time = time (NULL);
> if((current_time+PUBLISH_TIME)>local_time)
> {
> fireEvent();
> local_time=current_time;
> }
>
> }[/color]
There are probably numerous ways, but the most frequent I've
seen has been to do a poll or a select with a timeout and an
empty list of fd's. If your system supports it, you should use
nanosleep. (According to the Posix documentation, it is
optional. But most systems have it, I think.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34