opposite of Thread.wakeup

M

Mark Volkmann

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

Robert Klemme

Mark said:
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?MultiThreading
You need to invoke cond.wait and cond.signal. HTH

Kind regards

robert
 
R

Robert Klemme

Mark said:
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
 
A

Andrew Johnson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top