Perform function at each given time period

Asked

Viewed 62 times

2

ola i would like to implement a function in my program that performs a task every time having this code as an example something so I am wanting

void main(){
    while(1){
        if(passou 5 minutos){
            chama_funcao();
        }
    }
  • Have you heard about threads?

2 answers

1

I thought something like a counter, and putting the time according to the value that the variable a undergoes an increment.
The functionsleep(); works basically like a timer, so this function does a check every 1 second.

The code basically looks like this :

 void main(){
        int a = 0;
        while(1){
            a++;
            if(passou 5 minutos){
                chama_funcao();
            }
            sleep(1);
        }

1

One of the ways to do this is with the time() of time.h:

#include <time.h>

Code:

time_t tempoInicial;
time_t tempoAtual;
tempoInicial = time(NULL);
while (1){
    tempoAtual = time(NULL);
    if (tempoAtual / 60 > tempoInicial / 60 + 5){
        tempoInicial = time(NULL);
        //O que estiver aqui, será executado de 5 em 5 minutos
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.