0

I know clock() does not give wall-clock time, and CLOCKS_PER_SEC is system dependent value. Let's say I dont care system dependent issues, can I assume below code always waits 1 second in both GNU/Linux and Windows, because I tested it seemed ok to me. Or should I stick with system dependent sleep()/usleep() functions?

void wait_a_sec()
{
    clock_t start = clock();
    do
    {
        // wait 1 sec
        /**
         * Second call to clock() gives the processor time in clock ticks
         * since first call to it.
         * CLOCKS_PER_SEC: clock ticks per sec.
         */
    } while ( clock() < (start + CLOCKS_PER_SEC) );
}
Maksat Yalkabov
  • 548
  • 1
  • 8
  • 19
  • 3
    `sleep` is suspending the process. Your code is busy-waiting. – Eugene Sh. Mar 10 '21 at 21:24
  • Most POSIX systems (linux, *BSD) support `clock_gettime`, so I'd use that. Its resolution is [probably] 1ns. If you need Windows, see: https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows There are other pages/links if you Google: `"clock_gettime" Windows` – Craig Estey Mar 10 '21 at 21:30
  • I got your point but I want to implement my own sleep(1 sec) function. – Maksat Yalkabov Mar 10 '21 at 21:30
  • 1
    @MaksatYalkabov: In your previous comment, you say that you want to implement your own `sleep` function. However, that function has the property that it suspends the process while waiting. Your function, on the other hand, has the property that it "busy-waits", i.e. it uses 100% CPU resources while waiting instead of suspending the process while waiting. Is that what you want? – Andreas Wenzel Mar 10 '21 at 21:41
  • If we ignore the other issues, there is also the problem that on a 32-bit system this will wait zero seconds every 72 minutes due to integer overflow. –  Mar 10 '21 at 21:44
  • The function call `sleep(1)` will wait at least one second until your process gets rescheduled. However, under heavy-load situations, it may take significantly more time for your process to get rescheduled. Your busy-wait solution does not solve this problem, because your process may get [preempted](https://en.wikipedia.org/wiki/Preemption_(computing)#Preemptive_multitasking). Therefore, if you want to be sure that your program does not have to wait for much longer, then you may want to increase the priority of your process. In that case, you should certainly not be using a busy-wait, though. – Andreas Wenzel Mar 10 '21 at 21:50
  • @AndreasWenzel You explained the difference quiete clear. I made count down timer like pomodoro. So I need 1 sec of delay. But I wanted to do it myself just using C std functions :). I just wanted to be sure that whether SEC word in CLOCKS_PER_SEC means real second. – Maksat Yalkabov Mar 10 '21 at 21:56

1 Answers1

6

No. clock() gives you the CPU time used up by your program. Not time passed.

If for example you have one CPU and launch two instances of the program, it will wait until each has burned 1 second of active CPU time, so 2 seconds.

Also busy-waiting is generally a bad idea. Stick with sleep.