I want a program to wait for some moment in time, and resume operation
at that moment. sleep is ruled out because it just counts cycles and is
thrown off when my laptop suspends.
In C I’d use a one-fire timer, using setitimer. But in some quick
googling it looks like the ruby interpreter may use setitimer internally
for thread scheduling. Is there a safe way in ruby to sleep until a real
moment in time?
I want a program to wait for some moment in time, and resume operation
at that moment. sleep is ruled out because it just counts cycles and is
thrown off when my laptop suspends.
It’s going to have to be something like this I think. I don’t know how
much impact waking every so often (about once per second in my
situation) will have on power usage, but I see now that even setitimer
in C suffers the same problem sleep does with laptop suspend.
Looking at my code, it may be best to change the next-to-last line to:
sleep remaining if remaining > 0
Which OS are you using? On OS X the man page for setitimer claims
there are three separate timers, one of which uses real time. From
the man page:
The ITIMER_REAL timer decrements in real time. A SIGALRM signal
is
delivered when this timer expires.
I don’t have any experience with using this, so I’m left with the man
page.
Eric
Are you interested in on-site Ruby training that’s been highly
reviewed by former students? http://LearnRuby.com
remaining = end_time - Time.now
while granularity < remaining
sleep granularity
remaining = end_time - Time.now
end
sleep remaining
end
It’s going to have to be something like this I think. I don’t know how
much impact waking every so often (about once per second in my
situation) will have on power usage, but I see now that even setitimer
in C suffers the same problem sleep does with laptop suspend.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.