Vincent Fourmond
8/27/2006 8:17:00 AM
Hello !
I've recently had quite a fair bit of trouble with Ruby threads (on a
Linux box). I wrote three different programs (attached) that write
numbers from within threads:
* one with a standard puts
* one with a system "echo ..."
* one with a fork do system "echo ..." end
The first one behaves as expected (although the numbers are perfectly
ordered, which looks suspicious). In the second one, ruby never manages
to make a second system call in a thread (and finishes before the
subprograms are terminated). The third behaves a little better, but
crashes after, say, 3 to 4 system calls in a thread...
Is this true on other boxes ? Is it a flaw of my understanding of
threads, or a limitation inherent to the way threads are coded in Ruby ?
If that is so, it's really annoying - no way to delegate heavy tasks to
other well-written programs...
Thoughts ?
Vince
#!/usr/bin/ruby
10.times do |i|
Thread.start(i) do |i|
10.times do |j|
puts "#{i}.#{j}"
end
end
end
#!/usr/bin/ruby
10.times do |i|
Thread.start(i) do |i|
10.times do |j|
system "echo #{i}.#{j} "
end
end
end
#!/usr/bin/ruby
10.times do |i|
Thread.start(i) do |i|
10.times do |j|
fork do
system "echo #{i}.#{j} "
end
end
end
end