Strange behaviour with sleep() in a thread...

Discussion in 'Ruby' started by Just Another Victim of the Ambient Morality, Dec 24, 2008.

  1. I have a thread created with Thread.new that does some polling. I don't
    want it to busy wait so I call the sleep method like so:

    sleep(60)

    ...this should allow me to poll every minute which is good enough for my
    application. However, I'm finding that calling sleep from a thread in this
    manner often sleeps for a good deal longer than a minute. So far, I find it
    sleeping for about 8 or so minutes. The main thread is blocked in a gets
    call.
    What I'm actually trying to do is implement a simple timer. I'm trying
    to do something at a certain point in time, like a cron job, without busy
    waiting.
    Why is sleep behaving so strangely? Is there a simple way of doing what
    I'm trying to do?
    Thank you...
    Just Another Victim of the Ambient Morality, Dec 24, 2008
    #1
    1. Advertising

  2. Just Another Victim of the Ambient Morality

    Roger Pack Guest


    > Why is sleep behaving so strangely? Is there a simple way of doing
    > what

    If you're on 1.8.x then your two threads are actually one "real thread"
    running two green threads. One of the green threads [the gets] might be
    blocking and not allowing control to return to the other.
    Fix (should this be the case): make the one not block, or use 1.9
    Cheers!
    -=r
    --
    Posted via http://www.ruby-forum.com/.
    Roger Pack, Dec 27, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Miller
    Replies:
    3
    Views:
    3,947
    Stephen Miller
    Jul 2, 2004
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,375
  3. eftal
    Replies:
    6
    Views:
    950
    Thomas Hawtin
    Jan 25, 2006
  4. Sam Roberts
    Replies:
    16
    Views:
    363
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    213
    Richard
    May 22, 2007
Loading...

Share This Page