N
Nita Neou
Hi All
I got a little problem when using thread function and variable, let's
say this code :
require 'rubygems'
require 'watir'
require 'thread'
def testfunc(id)
total = id + 5
puts "id #{id} is #{total}"
end
threads = []
numberlist = [1, 2, 3]
for id in numberlist
threads << Thread.new {testfunc(id)}
end
threads.each { |aThread| aThread.join }
my output is :
[sophoah@fedora ]$ ruby test.rb
id 3 is 8
id 3 is 8
id 3 is 8
The fact is that I would like this output :
id 1 is 6
id 2 is 7
id 3 is 8
I am suspecting that all variable id, and total are all share among
all the thread, is that right ? why ? I thought variable in a block
were not share among variable ? How can I fix this problem ?
Thanks a lot for your help on it,
Regards
Nita
I got a little problem when using thread function and variable, let's
say this code :
require 'rubygems'
require 'watir'
require 'thread'
def testfunc(id)
total = id + 5
puts "id #{id} is #{total}"
end
threads = []
numberlist = [1, 2, 3]
for id in numberlist
threads << Thread.new {testfunc(id)}
end
threads.each { |aThread| aThread.join }
my output is :
[sophoah@fedora ]$ ruby test.rb
id 3 is 8
id 3 is 8
id 3 is 8
The fact is that I would like this output :
id 1 is 6
id 2 is 7
id 3 is 8
I am suspecting that all variable id, and total are all share among
all the thread, is that right ? why ? I thought variable in a block
were not share among variable ? How can I fix this problem ?
Thanks a lot for your help on it,
Regards
Nita