[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Sam Roberts

2/18/2005 3:17:00 AM

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



16 Answers

Assaph Mehr

2/18/2005 3:22:00 AM

0


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...

Luke Graham

2/18/2005 3:25:00 AM

0

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 <sroberts@uniserve.com> 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


Navindra Umanee

2/18/2005 3:33:00 AM

0

Sam Roberts <sroberts@uniserve.com> 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.


Shashank Date

2/18/2005 3:38:00 AM

0

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

Assaph Mehr

2/18/2005 3:52:00 AM

0


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

Assaph Mehr

2/18/2005 3:57:00 AM

0


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.

Sam Roberts

2/18/2005 4:07:00 AM

0

Quoteing navindra@cs.mcgill.ca, on Fri, Feb 18, 2005 at 12:33:13PM +0900:
> Sam Roberts <sroberts@uniserve.com> 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.
>


Navindra Umanee

2/18/2005 4:30:00 AM

0

Sam Roberts <sroberts@uniserve.com> 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.


Yukihiro Matsumoto

2/18/2005 4:50:00 AM

0

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 <sroberts@uniserve.com> 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.


Pit Capitain

2/18/2005 8:55:00 AM

0

Sam Roberts schrieb:
> 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?

Could it be that the delay variable is shared between the threads?

Regards,
Pit