Calling Thread.join blocks the current (main) thread. However not calling join results in all spawned threads to be killed when the main thread exits. How does one spawn persistent children threads in Ruby without blocking the main thread?
Here's a typical use of join.
for i in 1..100 do
puts "Creating thread #{i}"
t = Thread.new(i) do |j|
sleep 1
puts "Thread #{j} done"
end
t.join
end
puts "#{Thread.list.size} threads"
This gives
Creating thread 1 Thread 1 done Creating thread 2 Thread 2 done ... 1 threads
but I'm looking for how to get this
Creating thread 1 Creating thread 2 ... 101 threads Thread 1 done Thread 2 done ...
The code gives the same output in both Ruby 1.8.7 and 1.9.2