I have a computation that can be divided into independent units and the way I'm dealing with it now is by creating a fixed number of threads and then handing off chunks of work to be done in each thread. So in pseudo code here's what it looks like
# main thread
work_units.take(10).each {|work_unit| spawn_thread_for work_unit}
def spawn_thread_for(work)
Thread.new do
do_some work
more_work = work_units.pop
spawn_thread_for more_work unless more_work.nil?
end
end
Basically once the initial number of threads is created each one does some work and then keeps taking stuff to be done from the work stack until nothing is left. Everything works fine when I run things in irb but when I execute the script using the interpreter things don't work out so well. I'm not sure how to make the main thread wait until all the work is finished. Is there a nice way of doing this or am I stuck with executing sleep 10 until work_units.empty?
in the main thread
If you modify
spawn_thread_for
to save a reference to your createdThread
, then you can callThread#join
on the thread to wait for completion:produces:
(Stolen from the
ri Thread.new
documentation. See theri Thread.join
documentation for some more details.)So, if you amend
spawn_thread_for
to save the Thread references, you can join on them all:(Untested, but ought to give the flavor)
It seems like you are replicating what the Parallel Each (Peach) library provides.
You can use Thread#join
join(p1 = v1) public
Also you can use Enumerable#each_slice to iterate over the work units in batches
In ruby 1.9 (and 2.0), you can use
ThreadsWait
from the stdlib for this purpose: