# Ruby thread newbie problem

**URL:** <https://www.ruby-forum.com/t/ruby-thread-newbie-problem/119388>\
**Category:** Ruby\
**Created:** [November 6, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/ruby-thread-newbie-problem/119388 "2007-11-06T17:20:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bram\_Kuijper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@Bram\_Kuijper](https://www.ruby-forum.com/u/Bram_Kuijper)\
**Post date:** [November 6, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/ruby-thread-newbie-problem/119388/1 "2007-11-06T17:20:45Z")

</div>

Hi all,

As a ruby newbie, I am trying to have two Threads process the  
contents of one Queue. However, in my code only one Thread is processing  
the contents of Queue, instead of both Threads. How can I make sure that  
_both_ Threads process the Queue mentioned below?

This is my code:

queue = Queue.new  
queue \<\< “$contents\_of\_queue”  
queue \<\< “$contents\_of\_queue”  
queue \<\< “$contents\_of\_queue”

threads = []

2.times do |thread\_index|  
threads[thread\_index] =  
Thread.new(thread\_index) do | this\_thread\_name |

# let each Thread work with things from the queue

while ! queue.empty?

```
  contents_of_queue = queue.deq

  # output Thread number
  puts "executing #{contents_of_queue} " \
    + "by thread #{this_thread_name}"

end

```

end  
end

This code will output:  
“executing $contents\_of\_queue by thread 0”  
“executing $contents\_of\_queue by thread 0”  
“executing $contents\_of\_queue by thread 0”

my question is: why will thread 1 never show up?

thanks,  
Bram

---

<div class="post-metadata">

**Author:** ![Bram\_Kuijper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@Bram\_Kuijper](https://www.ruby-forum.com/u/Bram_Kuijper)\
**Post date:** [November 6, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/ruby-thread-newbie-problem/119388/2 "2007-11-06T19:01:21Z")

</div>

Bram Kuijper wrote:

> Hi all,
> 
> As a ruby newbie, I am trying to have two Threads process the  
> contents of one Queue. However, in my code only one Thread is processing  
> the contents of Queue, instead of both Threads. How can I make sure that  
> _both_ Threads process the Queue mentioned below?

It just that you haven’t given them much work to do, and Ruby’s thread  
scheduler has decided that this amount of work isn’t big enough to  
justify switching between threads (there’s a tradeoff between having  
very fine grained thread switching and higher throughput). You can  
increase the workload (more on the queue), or you can force the threads  
to switch more often, by using Thread.pass (below). But it’s probably  
better to let Ruby’s thread scheduler do that for you.

> ```
> threads[thread_index] =
> Thread.new(thread_index) do | this_thread_name |
>    
> # let each Thread work with things from the queue
> while ! queue.empty?   
> 
> contents_of_queue = queue.deq
> 
> ```

# Add this line to let other threads run (this is

# rarely necessary)

```
         Thread.pass

```

> ```
> # output Thread number
> puts "executing #{contents_of_queue} " \
> + "by thread #{this_thread_name}"
> 
> end
> end   
> 
> ```
> 
> end

# And don’t forget this, or else the main thread will quit even

# if the child threads are still working:

threads.each {|t| t.join}

---

<div class="post-metadata">

**Author:** ![Bram\_Kuijper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@Bram\_Kuijper](https://www.ruby-forum.com/u/Bram_Kuijper)\
**Post date:** [November 6, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/ruby-thread-newbie-problem/119388/3 "2007-11-06T19:48:15Z")

</div>

Bram Kuijper wrote:

> queue = Queue.new  
> # let each Thread work with things from the queue  
> end
> 
> This code will output:  
> “executing $contents\_of\_queue by thread 0”  
> “executing $contents\_of\_queue by thread 0”  
> “executing $contents\_of\_queue by thread 0”
> 
> my question is: why will thread 1 never show up?
> 
> thanks,  
> Bram

Thread 1 isn’t showing up because Thread 0 empties the queue before  
Thread 1 even gets to run.  
Before

while ! queue.empty?

add

puts “This is thread #{this\_thread\_name}”

and you’ll see that they do both run, it’s just the second thread  
doesn’t need to do anything.

-Justin
