Jez Fischer
Jez Fischer

Reputation: 657

Time delay in method delaying whole program

I have two methods, for example, delayloop() and function(), where delayloop() has a while and time() delay to loop it every n seconds.

If in my main, I call:

delayloop();

then

function();

The method function() is never called, as the looping method before it seems to prevents it.

What is the method to prevent the application from not passing the delayloop() function?

Edit:

delayloop();

void delayloop() {
boolean delay = true;
while (delay){
    time_t start_time, cur_time;

         time(&start_time);
         do
         {
                time(&cur_time);
       }
      while((cur_time - start_time) < 5);   

 cout << "+5 seconds" << endl;

}   

}

Upvotes: 2

Views: 710

Answers (4)

St&#233;phane
St&#233;phane

Reputation: 6905

replace the while loop by a for loop in delayloop().

(assuming the exit condition for the while loop is never met, and you would correct that in an easier to understand for loop... but this is a long shot without seeing the code and understanding the goal of these two functions)

Edit after you added the code:

The first while condition is always true (delay is not modified), this is why it blocks. replace it by an if.

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263118

Under normal conditions, control flows sequentially through your program. If you enter an endless loop before the function() line, then function is never going to get called. So it would seem you have an error in the delayloop function, and it would help is if we could see the code.

There we have it. You never set delay to false, hence we enter an endless loop.

Upvotes: 2

Beta
Beta

Reputation: 99094

You never change delay; there is no way out of the loop. It's hard to advise you how to change it, without knowing what you want delayloop() to do.

Upvotes: 0

BlackBear
BlackBear

Reputation: 22979

you are never changing the state of delay, so it will always stay true and the loop will be executed indefinitely.

Upvotes: 0

Related Questions