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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samuel
  • 574
  • 3
  • 7
  • 22
  • Does this answer your question? [Sleep for milliseconds](https://stackoverflow.com/questions/4184468/sleep-for-milliseconds) – Cris Luengo Jul 12 '21 at 15:36

5 Answers5

12

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;
}
MOnsDaR
  • 8,401
  • 8
  • 49
  • 70
  • I love the idea, however I am using Windows and I don't seem to have the file. – Samuel Nov 05 '10 at 06:09
  • Boost is available on Windows too, you just have to install the libs. [Getting Started Guide](http://www.boost.org/doc/libs/1_44_0/more/getting_started/windows.html) or download a [precompiled version](http://www.boostpro.com/download/) – MOnsDaR Nov 05 '10 at 06:10
  • Keep in mind that - in a multi-threaded environment -`boost::this_thread::sleep` adds an interruption point to your code. http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html – Martin Meeser Jul 18 '13 at 09:30
  • boost used for sleeping is outdated – Antonio Feb 11 '22 at 23:19
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
André
  • 460
  • 6
  • 18
  • 1
    Wow! Thanks, I didn't know it could be done in a platform-independant portable way :) – legends2k Nov 08 '12 at 10:21
  • 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. – Peter Mortensen Sep 09 '18 at 21:07
3

Use sleep rather than an empty while loop.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
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

OneOfOne
  • 95,033
  • 20
  • 184
  • 185