# Opposite of Thread.wakeup

**URL:** <https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747>\
**Category:** Ruby\
**Created:** [January 10, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747 "2006-01-10T14:23:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/1 "2006-01-10T14:23:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/2 "2006-01-10T14:44:38Z")

</div>

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

> **[WebMD - Better information. Better health.](https://www.webmd.com/?MultiThreading)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

You need to invoke cond.wait and cond.signal. HTH

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/3 "2006-01-10T15:05:54Z")

</div>

On 1/10/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://www.rubygarden.org/ruby?MultiThreading](http://www.rubygarden.org/ruby?MultiThreading)  
> 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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/4 "2006-01-10T17:05:23Z")

</div>

Quoting Mark V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/5 "2006-01-10T15:24:22Z")

</div>

Mark V. wrote:

> > You use a ConditionVariable. See the Queue example at  
> > [http://www.rubygarden.org/ruby?MultiThreading](http://www.rubygarden.org/ruby?MultiThreading)  
> > 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

```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 9:00am UTC](https://www.ruby-forum.com/t/opposite-of-thread-wakeup/60747/6 "2006-01-11T09:00:52Z")

</div>

On Tue, 10 Jan 2006 22:21:28 +0900, Mark V.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
