# Thread pool issue

**URL:** <https://www.ruby-forum.com/t/thread-pool-issue/226062>\
**Category:** Ruby\
**Created:** [March 18, 2010, 11:19am UTC](https://www.ruby-forum.com/t/thread-pool-issue/226062 "2010-03-18T11:19:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [March 18, 2010, 11:19am UTC](https://www.ruby-forum.com/t/thread-pool-issue/226062/1 "2010-03-18T11:19:35Z")

</div>

Hello,

```
 I was just trying out the thread pool code form Ruby Cookbook.

```

## I created a script to test it out. The complete code is as follows:

require ‘thread’

class ThreadPool  
def initialize(max\_size)  
@pool = []  
@max\_size = max\_size  
@pool\_mutex = Mutex.new  
@pool\_cv = ConditionVariable.new  
end

def dispatch(\*args)  
Thread.new do  
# Wait for space in the pool.  
@pool\_mutex.synchronize do  
while @pool.size \>= @max\_size  
puts “Pool is full; waiting to run #{args.join(’,’)}â€¦\n”  
# Sleep until some other thread calls @pool\_cv.signal.  
@pool\_cv.wait(@pool\_mutex)  
end  
end  
@pool \<\< Thread.current  
begin  
yield(\*args)  
rescue =\> e  
exception(self, e, \*args)  
ensure  
@pool\_mutex.synchronize do  
# Remove the thread from the pool.  
@pool.delete(Thread.current)  
# Signal the next waiting thread that there’s a space in the  
pool.  
@pool\_cv.signal  
end  
end  
end  
end

def shutdown  
@pool\_mutex.synchronize { @pool\_cv.wait(@pool\_mutex) until  
@pool.empty? }  
end

def exception(thread, exception, \*original\_args)  
# Subclass this method to handle an exception within a thread.  
puts “Exception in thread #{thread}: #{exception}”  
end  
end

@work = []  
def no\_work\_only\_sleep  
sleep(2)  
@work \<\< “Finished”  
end

pool = ThreadPool.new(3)

10.times do |i|  
pool.dispatch(i) do  
puts “Job #{i} started”  
no\_work\_only\_sleep  
end  
end

pool.shutdown  
puts @work.length

* * *

What I am doing here is creating 10 jobs and within a job calling a  
method.  
Inside the method I am inserting a string “Finished” to an array @work.  
So for 10 jobs this array should contain 10 such strings.

The issue I have here is : For a thread pool of size 3 it gives an  
output of 9 where it should have been 10.

For any other thread pool value it gives 10 as required.

Any reason as to why this is happening?

Thanks,  
Sriram.

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [March 18, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/thread-pool-issue/226062/2 "2010-03-18T13:39:49Z")

</div>

2010/3/18 Sriram V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> def initialize(max\_size)  
> while @pool.size \>= @max\_size  
> ensure
> 
> puts “Job #{i} started”  
> Inside the method I am inserting a string “Finished” to an array @work.  
> So for 10 jobs this array should contain 10 such strings.
> 
> The issue I have here is : For a thread pool of size 3 it gives an  
> output of 9 where it should have been 10.
> 
> For any other thread pool value it gives 10 as required.
> 
> Any reason as to why this is happening?

Two things stand out:

1. as far as I can see you are not properly synchronizing access to  
@work.

2. IMHO the check of the current thread pool size should be done in  
the calling thread in order to block it - not in the newly created  
background thread.

Issue 2 is a bit tricky to understand but I believe you have created a  
situation where background threads are started and waiting for room in  
the pool to become available but the shutdown may get in the way so  
you might terminate the pool although there are still threads waiting  
to start their work.

Kind regards

robert
