I am creating threads in a for loop, and I want to use the for loop’s i as the name for each particular thread. When I run this, instead of getting 1,2 or 2,1, I am getting 2,2. Is there a better/safer way to pass variables into a thread?
ts = []
for i in 1..2 do
ts.push( Thread.new(i) do
x = i
puts x
end)
end
ts.each do |t|
t.join()
end
You can pass variable through the block