Lurker News
Back to discussion

Comment by beerbajay

beerbajay6 replies#
The code currently given in the stream is `while [ "$(date)" != "Sat Oct 31 16:00:00 EDT 2015" ]; do \ sleep 1; done` ... so I hope the process wakes up on the correct millisecond.
udev#
Almost guaranteed not to, since 1 second sleep.
DanWaterworth1 reply#
How would you know if it did?
smtddr1 reply#
You may already have figured this out, but just incase anyone is wondering what the potential problem here is...

If it doesn't wake up and run the date command in a certain timeframe then this while loops will run forever.
The chances of that happening, while greater than zero, are really small though. It'd be something like the loop checks the time at "15:59:59", doesn't match so it goes to sleep just a few milliseconds before "16:00:00" then wakes up and runs the date command again and hopefully the deities of the CPU clock & scheduling will do this before too many milliseconds go by and the time becomes "16:00:01", at which point the while-loop would be infinite since the exact moment in time it's waiting for is now in the past and cannot occur again. I suspect though, CPU clock ticks are measured in nanoseconds or something smaller than a millisecond so this while-loop shouldn't miss its exit criteria on any modern machine.
Also, there's a chance this is just a fancy splash screen or even just a screenshot and a Twitch employee will manually get things started when it's time.

dllthomas#
"I suspect though, CPU clock ticks are measured in nanoseconds or something smaller than a millisecond so this while-loop shouldn't miss its exit criteria on any modern machine."

It's not a busy-wait. What matters is not CPU clock ticks, but how quickly an eligible-to-run program gets scheduled. That's still not going to be terribly long, of course.

boomlinde1 reply#
Why would it have to be the exact millisecond? "$(date)" will have that value for 1 second, assuming the format is correct. There is some very small timespan in which it could fail, but the chances are very much in favor of successfully exiting the loop.
arjie2 replies#
`sleep` sleeps for seconds.
tlrobinson2 replies#
And `date` returns a timestamp with seconds precision.

`time sh -c 'if [ "$(date)" != "Sat Oct 31 16:00:00 EDT 2015" ]; then sleep 1; fi'` took around 1.010 seconds on my machine, but that includes starting a new shell, so there's probably a much smaller than 1% chance that that particular second will be missed.
EDIT: here's a little program to test this:

    #!/usr/bin/env bash
    while true; do
      target=$(expr $(date +%s) + 2)
      while true; do
        now=$(date +%s)
        if [ "$now" != "$target" ]; then
          if [ "$now" -gt "$target" ]; then
            echo "FAIL :("
            break
          else
            sleep 1
          fi
        else
          echo "OK! :)"
          break
        fi
        sleep 0.1
      done
    done
Adjust the "sleep 0.1" to increase or decrease the perecentage of failures. Remove to see that it (almost) never fails.

Continue this thread — 2 replies

untog#
Yes, but presumably it rounds that last number, right? So as long as it ticks somewhere between 15:59:59.50 and 16:00:00.49 it should run?
udev1 reply#
Many solutions, e.g. make sleep 0.9 sec, i.e. less than period to guarantee entering the critical 1 second span at least once.
jerf2 replies#
Even if you set the interval to the minimum possible, there is no upper bound on how long it may take to complete, or indeed, any guarantee that it will complete. In this case it doesn't matter, but I point this out because I've seen people trying to use this sort of logic in places where it does matter in the last three months.
JonnieCache1 reply#
You're talking about `sleep` specifically here, right? Or general use of timers/polling for flow control?

Continue this thread — 1 reply

ultramancool1 reply#
If they'd just switched it to greater than and used a unix timestamp...

Continue this thread — 1 reply

jawr#
Pretty sure it's just a splash screen, but yeah :p
sixothree2 replies#
It's 2015. How is this kind of thing still acceptable?
tlrobinson#
Because this isn't mission critical code, and it will work 99+% of the time. If it doesn't work the author can manually intervene.
cylon13#
It's a splash screen. It's acceptable for the same reason the CSS on your coffee cup doesn't have to parse without error.