我正在使用多个线程(例如 10 个线程)调用同一个 Ruby 函数。每个线程将不同的参数传递给函数。
例子:
def test thread_no
puts "In thread no." + thread_no.to_s
end
num_threads = 6
threads=[]
for thread_no in 1..num_threads
puts "Creating thread no. "+thread_no.to_s
threads << Thread.new{test(thread_no)}
end
threads.each { |thr| thr.join }
输出:
创建线程号1个
创建线程号2个
创建线程号3个
创建线程号4个
在线程 4
创建线程号5个
创建线程号6个
在第 6 号线程中
在第 6 号线程中
在第 6 号线程中
在第 6 号线程中
在线程 6 中
当然我想得到输出:在线程号。 1 (2,3,4,5,6) 我能以某种方式实现它吗?
最佳答案
问题是 for
循环。在 Ruby 中,它重用了单个变量。
所以线程体的所有 block 都访问同一个变量。 this 变量在循环结束时为 6。线程本身可能仅在循环结束后启动。
您可以使用each
循环来解决这个问题。它们的实现更加清晰,每个循环变量都独立存在。
(1..num_threads).each do | thread_no |
puts "Creating thread no. "+thread_no.to_s
threads << Thread.new{test(thread_no)}
end
不幸的是,ruby 中的for
循环是一个令人惊讶的来源。所以最好始终使用 each
循环。
添加:
您还可以为 Thread.new
提供一个或多个参数,这些参数将传递到线程主体 block 中。通过这种方式,您可以确保该 block 在其自身范围之外不使用任何变量,因此它也适用于 for 循环。
threads << Thread.new(thread_no){|n| test(n) }
关于Ruby 线程使用不同的参数调用相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625574/