开发者

is there a loop controlled by time in c++?

开发者 https://www.devze.com 2023-01-14 05:01 出处:网络
I am wondering if I can loop x times in one minute interval between 开发者_开发百科each loop. for (int x = 10; x > 0; x--)

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


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.


I think this should work:

#include <sleep.h>
...

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


The only standard way is to use the sleep function.

for (int x = 10; x > 0; x--)
{
     cout << "BOOM" << endl;
     sleep(60); // 1 minute
}


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.


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.


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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号