views:

121

answers:

7

Hi,

I am wondering if I can loop x times in one minute interval between each loop.

for (int x = 10; x > 0; x--)
{
     cout << "BOOM" << endl;
}

Is there any way I can print boom every one minute? Or there is a better way to do this?

Thank you

A: 

I think this should work:

#include <sleep.h>
...

for (int x = 10; x > 0; x--)
{
    cout << "BOOM" << endl;
    sleep(60);
}
quadelirus
i think this will work for me since it is only a simulation program for school work, thank you guys
chandra wib
A: 

The only standard way is to use the sleep function.

for (int x = 10; x > 0; x--)
{
     cout << "BOOM" << endl;
     sleep(60); // 1 minute
}
jojaba
This looks like sleep(3) from POSIX, which takes seconds. Windows's millisecond Sleep is spelled with a capital S.
d0k
Sleep usually works with milliseconds.
rursw1
+3  A: 

Standard C++ has no such function. The closest thing you could do is have an infinite loop constantly asking if a minute has gone by.

The draft C++0x standard has sleep_until() and sleep_for() under the header <thread>, but your implementation may not support these features yet (they aren't standard yet anyway), and it may be more work than it's worth.

Consult your implementation documentation. There's probably a sleep() function or something like that, and you could put something like sleep(60) in your loop.

David Thornley
Small note: we are not using real time OSes, therefore it will be about 60 seconds and there isn't much choice for doing better.
Matthieu M.
A: 

You should use timers or at least measure time... you can use this for millisecond-precision:

/* microtimer.c - function to get microsecond exact times for measure */

/* System specific headers */
#ifdef WIN32
    #include <windows.h>
#else
    #include <sys/time.h>
#endif

double getMicroTime(void) {
#ifdef WIN32
        LARGE_INTEGER frequency; /* ticks per second */
        LARGE_INTEGER time;

        QueryPerformanceFrequency(&frequency);
        QueryPerformancetimeer(&time);

        return time.QuadPart * (1000000.0 / frequency.QuadPart);
#else
        struct timeval time;

        gettimeofday(&time,0); /* 0 instead of NULL to reduce stdlib */

        return (time.tv_sec * 1000000.0) + time.tv_usec;
#endif
}
apirogov
First, that's Microsoft Windows specific. Second, it doesn't address the question of how to wait a minute. Knowing what time it is to the nearest millisecond isn't very useful in taking a sixty-second nap.
David Thornley
@David Thornley: Its NOT specific, thats the whole point of posting it! <sys/time.h> is for the equivalent on unix! I thought that it might be useful to have such a wrapper, who knows? its precise and might come in handy for the person... and you CAN use it to solve the problem!
apirogov
A: 

I don't believe there is any native construct in C++ that lets you do this. There's the sleep() function which others have shown can be used in a loop. You might also want to look into finding a real-time library that may have better capabilities if your needs are that strict (unfortunately, I haven't worked in that for a long time so I don't know the current good ones). Some threading libraries may also offer this, if you don't mind creating threads for your timed procedures.

FrustratedWithFormsDesigner
A: 

If you want to loop something depending on the time elapsed, you might find timers useful. Timers are available in most of the APIs, such as Win32API.

rhino
A: 

As rhino said, timers are most likely what you want to use.

If this is a console application, then the sleep()-style delays may be acceptable.

However, most apps these days are GUI applications, and for those you will NOT want to block a process sitting on a sleep() call.

What you'll want to do is set up a timer, and in a timer event handler you perform one pass of your action. When the last pass is done, disable the timer. This allows the GUI event loop to continue processing events, and it keeps your application from appearing to lock up while it's asleep.

If you need better time precision, you can also run the sleep() method in a worker thread. However, you're not going to get time precision better than 10-50ms (potentially much worse) with any normal method. This should be perfectly fine for most uses.

If you need good cumulative time tracking (say you're drawing the hands on a clock), you should get the current system time inside your timer or worker thread and use that number to adjust how long you wait for the next pass. Otherwise, since timers (and sleep()) will be triggered "as soon as possible after the timer/sleep duration is complete", you'll slowly add up these extra delays and it will run slower than you intended. Ex: sleep(1000) may sleep for 1000 ms, or more likely something like 1005, 1015, 1021, etc. Timers will behave similarly.

There are many ways to do timers (Win32, Qt, boost, etc) and an equally large number of ways to manage threading. So, we'd need to know more about what sort of platform you are working with to answer further.

darron