Reputation: 1
I'm new to ruby, and want to use thread. In purpose, I want thread to spawn thread, I have below code:
require 'thread'
semaphore = Mutex.new
thr = Array.new
outputs = Array.new
scripts = Array.new
for i in 1..3
thr[i] = Thread.new do
puts "adding #{i} thread\n"
puts "ready to create #{i} thread\n"
scripts[i]= Thread.new do
puts "in #{i} thread\n"
puts "X#{i}\n"
outputs[i] = "a#{i}"
end
end
end
for i in 1..3
thr[i].join
end
for i in 1..3
scripts[i].join
end
for i in 1..3
puts outputs[i]
end
The output is
adding 1 thread
adding 2 thread
adding 3 thread
ready to create 3 thread
ready to create 1 thread
ready to create 1 thread
in 1 thread
in 1 thread
in 2 thread
X2
X3
X1
C:/Users/user/workspace/ruby-test/test.rb:61: undefined method `join' for nil:NilClass (NoMethodError)
from C:/Users/liux14/workspace/ruby-test/test.rb:60:in `each'
from C:/Users/liux14/workspace/ruby-test/test.rb:60
The first three lines are correct, but following i is messed up.
two i = 1 and one i = 2 and one i = 3. And the one of output[i] is nil.
What I missed?
Upvotes: 0
Views: 205
Reputation: 78463
Your use of the for i in 1..3
statement might be making i
available outside of the for block, and makes it shared across the parent and child threads.
Try it with a block instead:
(1..3).each do |i|
# code
end
#!/usr/bin/env ruby
require 'thread'
semaphore = Mutex.new
thr = Array.new
outputs = Array.new
scripts = Array.new
(1..3).each do |i|
thr[i] = Thread.new do
puts "adding #{i} thread\n"
puts "ready to create #{i} thread\n"
scripts[i]= Thread.new do
puts "in #{i} thread\n"
puts "X#{i}\n"
outputs[i] = "a#{i}"
end
end
end
(1..3).each do |i|
thr[i].join
end
(1..3).each do |i|
scripts[i].join
end
(1..3).each do |i|
puts outputs[i]
end
denis@DB:~/wk $ ./test.rb
adding 1 thread
ready to create 1 thread
adding 3 thread
ready to create 3 thread
adding 2 thread
ready to create 2 thread
in 1 thread
X1
in 3 thread
in 2 thread
X3
X2
a1
a2
a3
Upvotes: 1