[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

opposite of Thread.wakeup

Mark Volkmann

1/10/2006 1:21:00 PM

If I create a thread and don't want it to be eligible to run until I
decide to make it eligible later, how do I make it sleep? In other
words, what's the opposite of Thread.wakeup?

Thread::stop stops the execution of the current thread. I need a way
to stop execution of a thread that is not the current thread so that
it won't be scheduled.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


5 Answers

Robert Klemme

1/10/2006 1:40:00 PM

0

Mark Volkmann wrote:
> If I create a thread and don't want it to be eligible to run until I
> decide to make it eligible later, how do I make it sleep? In other
> words, what's the opposite of Thread.wakeup?
>
> Thread::stop stops the execution of the current thread. I need a way
> to stop execution of a thread that is not the current thread so that
> it won't be scheduled.

You use a ConditionVariable. See the Queue example at
http://www.rubygarden.org/ruby?Mult...
You need to invoke cond.wait and cond.signal. HTH

Kind regards

robert


Mark Volkmann

1/10/2006 2:05:00 PM

0

On 1/10/06, Robert Klemme <bob.news@gmx.net> wrote:
> Mark Volkmann wrote:
> > If I create a thread and don't want it to be eligible to run until I
> > decide to make it eligible later, how do I make it sleep? In other
> > words, what's the opposite of Thread.wakeup?
> >
> > Thread::stop stops the execution of the current thread. I need a way
> > to stop execution of a thread that is not the current thread so that
> > it won't be scheduled.
>
> You use a ConditionVariable. See the Queue example at
> http://www.rubygarden.org/ruby?Mult...
> You need to invoke cond.wait and cond.signal. HTH

That will definitely work, but I was hoping for something simpler like
a Thread.stop instance method. I suppose that isn't supported because
it is considered unsafe for the same reason it is deprecated in Java.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


Robert Klemme

1/10/2006 2:20:00 PM

0

Mark Volkmann wrote:
> On 1/10/06, Robert Klemme <bob.news@gmx.net> wrote:
>> Mark Volkmann wrote:
>>> If I create a thread and don't want it to be eligible to run until I
>>> decide to make it eligible later, how do I make it sleep? In other
>>> words, what's the opposite of Thread.wakeup?
>>>
>>> Thread::stop stops the execution of the current thread. I need a way
>>> to stop execution of a thread that is not the current thread so that
>>> it won't be scheduled.
>>
>> You use a ConditionVariable. See the Queue example at
>> http://www.rubygarden.org/ruby?Mult...
>> You need to invoke cond.wait and cond.signal. HTH
>
> That will definitely work, but I was hoping for something simpler like
> a Thread.stop instance method. I suppose that isn't supported because
> it is considered unsafe for the same reason it is deprecated in Java.

Depending on your application you can use the std library's Queue. Client
push tasks into the queue and you have a processor that blocks if it's
trying to read from an empty queue.

robert

MenTaLguY

1/10/2006 4:04:00 PM

0

Quoting Mark Volkmann <r.mark.volkmann@gmail.com>:

> If I create a thread and don't want it to be eligible to run
> until I decide to make it eligible later, how do I make it sleep?
> In other words, what's the opposite of Thread.wakeup?
>
> Thread::stop stops the execution of the current thread. I need a
> way to stop execution of a thread that is not the current thread
> so that it won't be scheduled.

Stop it at what point in its execution, though? You don't want to
go stopping it at some random place, where it might be holding a
lock.

The best way to do this is to establish some communication channel
between the threads, sending a message to the thread in response to
which it can call Thread::stop in a known-safe place.

-mental


Andrew Johnson

1/11/2006 7:54:00 AM

0

On Tue, 10 Jan 2006 22:21:28 +0900, Mark Volkmann
<r.mark.volkmann@gmail.com> wrote:

> If I create a thread and don't want it to be eligible to run until I
> decide to make it eligible later, how do I make it sleep? In other
> words, what's the opposite of Thread.wakeup?
>
> Thread::stop stops the execution of the current thread. I need a way
> to stop execution of a thread that is not the current thread so that
> it won't be scheduled.

If you want to stop it right at the beginning then perhaps this
might be sufficient for your purposes:

thr = Thread.new {Thread.stop; puts "thr running now"}
# stuff
thr.run

andrew