Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > pulling my hair out, why won't Kernel.sleep(0) sleep?

Reply
Thread Tools

pulling my hair out, why won't Kernel.sleep(0) sleep?

 
 
Sam Roberts
Guest
Posts: n/a
 
      02-18-2005
Can anybody give me any hints as to what I should be looking for? What
can cause sleep(0) to wake up?

I've got a thread that does

loop do
sleep(delay)

... do stuff, then set delay to 0 because there is no more work ...

delay = 0
end


And my loop is busy looping, sleep just won't!

Whether it does or not is triggered by changes in what appear to me to
be completely unrelated code. I'm mystified.

Thanks,
Sam



 
Reply With Quote
 
 
 
 
Assaph Mehr
Guest
Posts: n/a
 
      02-18-2005

Sam Roberts wrote:
> Can anybody give me any hints as to what I should be looking for?

What
> can cause sleep(0) to wake up?
>
> I've got a thread that does
>
> loop do
> sleep(delay)
>
> ... do stuff, then set delay to 0 because there is no more work

....
>
> delay = 0
> end
>
>
> And my loop is busy looping, sleep just won't!
>
> Whether it does or not is triggered by changes in what appear to me

to
> be completely unrelated code. I'm mystified.


Silly question: Is there another thread active at the same time? I
don't think a thread can sleep if it's the only one...

 
Reply With Quote
 
 
 
 
Luke Graham
Guest
Posts: n/a
 
      02-18-2005
From the docs - SIGALRM or another thread calling run. If these cases
arent relevant, could you post an example of the unrelated code?


On Fri, 18 Feb 2005 12:16:41 +0900, Sam Roberts <> wrote:
> Can anybody give me any hints as to what I should be looking for? What
> can cause sleep(0) to wake up?
>
> I've got a thread that does
>
> loop do
> sleep(delay)
>
> ... do stuff, then set delay to 0 because there is no more work ...
>
> delay = 0
> end
>
> And my loop is busy looping, sleep just won't!
>
> Whether it does or not is triggered by changes in what appear to me to
> be completely unrelated code. I'm mystified.
>
> Thanks,
> Sam
>
>



--
spooq


 
Reply With Quote
 
Navindra Umanee
Guest
Posts: n/a
 
      02-18-2005
Sam Roberts <> wrote:
> Can anybody give me any hints as to what I should be looking for? What
> can cause sleep(0) to wake up?


The documentation is wrong.

sleep(0) returns immediately. sleep with *zero* arguments sleeps
forever.

static VALUE
rb_f_sleep(argc, argv)
int argc;
VALUE *argv;
{
int beg, end;

beg = time(0);
if (argc == 0) {
rb_thread_sleep_forever();
}
else if (argc == 1) {
rb_thread_wait_for(rb_time_interval(argv[0]));
}
else {
rb_raise(rb_eArgError, "wrong number of arguments");
}

end = time(0) - beg;

return INT2FIX(end);
}

Cheers,
Navin.


 
Reply With Quote
 
Shashank Date
Guest
Posts: n/a
 
      02-18-2005
Assaph Mehr wrote:

> Silly question: Is there another thread active at the same time? I
> don't think a thread can sleep if it's the only one...


Huh? You mean the main thread cannot sleep? Or have I mis-read your answer?

-- shanko
 
Reply With Quote
 
Assaph Mehr
Guest
Posts: n/a
 
      02-18-2005

Shashank Date wrote:
> Assaph Mehr wrote:
>
> > Silly question: Is there another thread active at the same time? I
> > don't think a thread can sleep if it's the only one...

>
> Huh? You mean the main thread cannot sleep? Or have I mis-read your

answer?

Perhaps I wasn't clear. The main thread can of course sleep for a
specified period. But if you do a sleep 0, it means sleep until woken
up. Ruby will check, see that there is no other thread waiting and will
thus wake the main thread again.
So if you only have one thread, you can't sleep indefinitely.

Assaph

 
Reply With Quote
 
Assaph Mehr
Guest
Posts: n/a
 
      02-18-2005

Assaph Mehr wrote:
> Shashank Date wrote:
> > Assaph Mehr wrote:
> >
> > > Silly question: Is there another thread active at the same time?

I
> > > don't think a thread can sleep if it's the only one...

> >
> > Huh? You mean the main thread cannot sleep? Or have I mis-read your

> answer?
>
> Perhaps I wasn't clear. The main thread can of course sleep for a
> specified period. But if you do a sleep 0, it means sleep until woken
> up. Ruby will check, see that there is no other thread waiting and

will
> thus wake the main thread again.
> So if you only have one thread, you can't sleep indefinitely.


Oops, looks like I was wrong. See Navindra's clarification.

 
Reply With Quote
 
Sam Roberts
Guest
Posts: n/a
 
      02-18-2005
Quoteing , on Fri, Feb 18, 2005 at 12:33:13PM +0900:
> Sam Roberts <> wrote:
> > Can anybody give me any hints as to what I should be looking for? What
> > can cause sleep(0) to wake up?

>
> The documentation is wrong.
>
> sleep(0) returns immediately. sleep with *zero* arguments sleeps
> forever.


Thank you, thank you, thank you!

I had started to notice that sometimes the code busy loops, and
sometimes it doesn't, it doesn't matter whether I make any code changes,
that turned out to be unrelated.

Do you have any idea why this should be? What does Kernel.sleep(0)
actually do?

Sam

>
> static VALUE
> rb_f_sleep(argc, argv)
> int argc;
> VALUE *argv;
> {
> int beg, end;
>
> beg = time(0);
> if (argc == 0) {
> rb_thread_sleep_forever();
> }
> else if (argc == 1) {
> rb_thread_wait_for(rb_time_interval(argv[0]));
> }
> else {
> rb_raise(rb_eArgError, "wrong number of arguments");
> }
>
> end = time(0) - beg;
>
> return INT2FIX(end);
> }
>
> Cheers,
> Navin.
>



 
Reply With Quote
 
Navindra Umanee
Guest
Posts: n/a
 
      02-18-2005
Sam Roberts <> wrote:
> I had started to notice that sometimes the code busy loops, and
> sometimes it doesn't, it doesn't matter whether I make any code changes,
> that turned out to be unrelated.
>
> Do you have any idea why this should be? What does Kernel.sleep(0)
> actually do?


I would guess that the sleep is triggering the thread scheduler to
check the queue for eligible threads to run. Probably sometimes the
same thread is chosen and sometimes another thread.

Not 100% sure.

Cheers,
Navin.


 
Reply With Quote
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      02-18-2005
Hi,

In message "Re: pulling my hair out, why won't Kernel.sleep(0) sleep?"
on Fri, 18 Feb 2005 13:07:28 +0900, Sam Roberts <> writes:

|> The documentation is wrong.

Oops. I will fix.

|I had started to notice that sometimes the code busy loops, and
|sometimes it doesn't, it doesn't matter whether I make any code changes,
|that turned out to be unrelated.
|
|Do you have any idea why this should be? What does Kernel.sleep(0)
|actually do?

I guess it was busy looping, but there were other threads to work
sometimes. sleep(0) does not sleep but causes context switch.

matz.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hair Loss ? This Will Make Your Hair Grow Again ! Nicolas Python 0 04-24-2009 11:09 AM
(Hair Loss) Natural 100% Proven Techniques To Grow Hair rogerp C Programming 0 03-22-2009 07:45 AM
(Hair Loss) Natural 100% Proven Techniques To Grow Hair rogerp Python 0 03-22-2009 07:45 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
hair loss help, grow hair again for better photos thehairlossman@operamail.com Digital Photography 13 03-19-2006 01:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57