user2976568
user2976568

Reputation: 149

How do i stop a while loop after a certain time in C

How do i stop a while loop after a certain time in C, i did it in c++ and i tried converting that to c (below) but it didnt work

#include <time.h>

int main(void)
{
    time_t endwait;
    time_t start;

    endwait = start + seconds ;
    while (start < endwait)
    {
        /* Do stuff while waiting */
    }
}

Upvotes: 4

Views: 25800

Answers (4)

isayme
isayme

Reputation: 319

How about try my test code:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
    time_t endwait;
    time_t start = time(NULL);
    time_t seconds = 10; // end loop after this time has elapsed

    endwait = start + seconds;

    printf("start time is : %s", ctime(&start));

    while (start < endwait)
    {
        /* Do stuff while waiting */
        sleep(1);   // sleep 1s.
        start = time(NULL);
        printf("loop time is : %s", ctime(&start));
    }

    printf("end time is %s", ctime(&endwait));

    return 0;
}

A example outprint is :

wugq@SSDEV016:~/tools/test $ ./a.out
start time is : Fri Jan 17 17:12:57 2014
loop time is : Fri Jan 17 17:12:58 2014
loop time is : Fri Jan 17 17:12:59 2014
loop time is : Fri Jan 17 17:13:00 2014
loop time is : Fri Jan 17 17:13:01 2014
loop time is : Fri Jan 17 17:13:02 2014
loop time is : Fri Jan 17 17:13:03 2014
loop time is : Fri Jan 17 17:13:04 2014
loop time is : Fri Jan 17 17:13:05 2014
loop time is : Fri Jan 17 17:13:06 2014
loop time is : Fri Jan 17 17:13:07 2014
end time is Fri Jan 17 17:13:07 2014

Upvotes: 5

NabilSolo
NabilSolo

Reputation: 1

If you're on linux, you should consider (depending on your application) to use signals, check alarm function and signal (this one to define the handler, which will define the behaviour of your program once a signal is received).

You'll find details in the man pages :

$ man signal
...
$ man alarm
...

Upvotes: 0

Parthiv Shah
Parthiv Shah

Reputation: 360

Please give a full example. I think you have to use time() to get the approximate current time..After that you can go ahead.

Upvotes: 0

abligh
abligh

Reputation: 25119

In C, there are no constructors, so time_t start; just declares a variable of type time_t. It does not set that equal to the current time. You thus need to read the current time before the loop (and assign that to starttime), and read it within the loop (or within the while condition).

Also, the loop should be

while ((currenttime = [code to assign time]) < endwait)
{
    ...
}

or neater

while ([code to assign time] < endwait)
{
    ...
}

IE look at currenttime, not starttime.

In order to read the time, you want to use time(NULL) (if you are using time values in seconds. So your completed program would look something like (untested):

#include <time.h>

int main(void)
{
    time_t endwait;
    int seconds = 123;

    endwait = time (NULL) + seconds ;
    while (time (NULL) < endwait)
    {
        /* Do stuff while waiting */
    }
}

Note also that if the number of seconds is small, you might want to use the higher precision gettimeofday. This will prevent waiting for 1 second waiting for anything from 0 seconds to 1.

Upvotes: 8

Related Questions