Forum: Ruby Ruby Sleep

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ricardo G. (Guest)
on 2008-10-10 19:50
Hi all

Ruby sleep is based on the system time, and it doesn't work well for me,
since my system time can fluctuate.

I need a sleep based on cpu ticks, or something like this, does ruby has
a way to do this? Anyone has an alternative for it, or a ruby sleep
implementation based on ticks?

Thanks!
Dave T. (Guest)
on 2008-10-10 19:55
(Received via mailing list)
On Oct 10, 2008, at 10:47 AM, Ricardo Giorni wrote:

> I need a sleep based on cpu ticks, or something like this, does ruby
> has
> a way to do this? Anyone has an alternative for it, or a ruby sleep
> implementation based on ticks?

Does your system support select()?
Ricardo G. (Guest)
on 2008-10-10 21:22
> Does your system support select()?

yes
Joel VanderWerf (Guest)
on 2008-10-10 21:33
(Received via mailing list)
Dave T. wrote:
>
> On Oct 10, 2008, at 10:47 AM, Ricardo Giorni wrote:
>
>> I need a sleep based on cpu ticks, or something like this, does ruby has
>> a way to do this? Anyone has an alternative for it, or a ruby sleep
>> implementation based on ticks?
>
> Does your system support select()?

Doesn't #sleep use select() anyway, via rb_thread_wait_for()?
Joel VanderWerf (Guest)
on 2008-10-10 21:35
(Received via mailing list)
Joel VanderWerf wrote:
> Doesn't #sleep use select() anyway, via rb_thread_wait_for()?
Apparently so:

$ strace ruby -e '3.times {sleep 1}' 2>&1 | tail
time(NULL)                              = 1223660042
time(NULL)                              = 1223660042
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
time(NULL)                              = 1223660043
time(NULL)                              = 1223660043
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
time(NULL)                              = 1223660044
rt_sigaction(SIGINT, {SIG_DFL}, {0x80b5320, [], 0}, 8) = 0
exit_group(0)                           = ?
Process 6665 detached
Robert K. (Guest)
on 2008-10-11 02:15
(Received via mailing list)
On 10.10.2008 17:47, Ricardo Giorni wrote:
> Ruby sleep is based on the system time, and it doesn't work well for me,
> since my system time can fluctuate.

What exactly is this supposed to mean?  Does your system clock change
arbitrarily?  That sounds like a very bad thing to have and I would try
to fix it.

Cheers

  robert
Charles Oliver N. (Guest)
on 2008-10-11 03:45
(Received via mailing list)
Robert K. wrote:
> On 10.10.2008 17:47, Ricardo Giorni wrote:
>> Ruby sleep is based on the system time, and it doesn't work well for me,
>> since my system time can fluctuate.
>
> What exactly is this supposed to mean?  Does your system clock change
> arbitrarily?  That sounds like a very bad thing to have and I would try
> to fix it.

System clocks are not generally guaranteed to be accurate to the same
level that processor ticks guarantee. So you can run into cases where
short sleeps never happen or run arbitrarily too long.

Of course in Ruby (MRI) thread scheduling is largely cooperative, so the
OP is probably expecting a lot out of it anyway.

- Charlie
This topic is locked and can not be replied to.