Need a better wait solution
Asked Answered
T

5

3

Recently I have been writing a program in C++ that pings three different websites and then depending on pass or fail it will wait 5 minutes or 30 seconds before it tries again.

Currently I have been using the ctime library and the following function to process my waiting. However, according to my CPU meter this is an unacceptable solution.

void wait (int seconds)
{
   clock_t endwait;
   endwait = clock () + seconds * CLOCKS_PER_SEC;
   while (clock () < endwait) {}
}

The reason why this solution is unacceptable is because according to my CPU meter the program runs at 48% to 50% of my CPU when waiting. I have a Athlon 64 x2 1.2 GHz processor. There is no way my modest 130 line program should even get near 50%.

How can I write my wait function better so that it is only using minimal resources?

Togetherness answered 5/11, 2010 at 5:2 Comment(1)
Does this answer your question? Sleep for millisecondsPseudohemophilia
P
12

Update in 2023: Nowadays you'd probably not use Boot anymore for this task since the C++ Standard library added native support for sleeping back in C++11. Please check the more appropriate answers below.

To stay portable you could use Boost::Thread for sleeping:

#include <boost/thread/thread.hpp>

int main()
{
    //waits 2 seconds
    boost::this_thread::sleep( boost::posix_time::seconds(1) );
    boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );

    return 0;
}
Pya answered 5/11, 2010 at 5:47 Comment(4)
I love the idea, however I am using Windows and I don't seem to have the <boost/thread/thread.hpp> file.Togetherness
Boost is available on Windows too, you just have to install the libs. Getting Started Guide or download a precompiled versionPya
Keep in mind that - in a multi-threaded environment -boost::this_thread::sleep adds an interruption point to your code. boost.org/doc/libs/1_49_0/doc/html/thread/…Unweave
boost used for sleeping is outdatedPinot
M
6

With the C++11 standard the following approach can be used:

std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::seconds(100));

Alternatively sleep_until could be used.

Minstrel answered 15/3, 2012 at 11:16 Comment(2)
Wow! Thanks, I didn't know it could be done in a platform-independant portable way :)Hermon
Yes, but perhaps the time resolution is not 1 ms, but 16-17 ms. You might be lucky with 100 ms, but specifying 101 ms may actually result in 117 ms. The relative error would be much worse for low values, e.g. specifying 3 ms may result in an actual sleep time of 17 ms. In other words, what might be acceptable for sleep in seconds breaks down for sleep in low milliseconds.Parkman
C
3

Use sleep rather than an empty while loop.

Catron answered 5/11, 2010 at 5:7 Comment(0)
F
3

Just to explain what's happening: when you call clock() your program retrieves the time again: you're asking it to do that as fast as it can until it reaches the endtime... that leaves the CPU core running the program "spinning" as fast as it can through your loop, reading the time millions of times a second in the hope it'll have rolled over to the endtime. You need to instead tell the operating system that you want to be woken up after an interval... then they can suspend your program and let other programs run (or the system idle)... that's what the various sleep functions mentioned in other answers are for.

Feisty answered 5/11, 2010 at 6:12 Comment(0)
W
0

There's Sleep in windows.h, on *nix there's sleep in unistd.h.

There's a more elegant solution @ http://www.faqs.org/faqs/unix-faq/faq/part4/section-6.html

Westphalia answered 5/11, 2010 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.