Robert Klemme
12/4/2006 9:52:00 PM
On 04.12.2006 22:07, Alex Ciarlillo wrote:
> I am trying to run 3 threads of a process using 3 sets of data. The
> process is run from a class I made but for some reason when this certain
> class method is called it seems to be getting the same data from each
> thread. Here is the code I am using to create the threads:
>
> threads = []
> breakfast = ['breakfast', '12:00:a', '11:00:a']
> lunch = ['lunch', '11:01:a', '4:00:p']
> dinner = ['dinner', '4:01:p', '11:59:p']
> periods = [breakfast, lunch, dinner]
> locations = [0,1,2,3,4,5]
> for period in periods
> threads << Thread.new(period) {
You need to use a block parameter for "period" otherwise you will run
into this issue:
irb(main):035:0* for i in 1..5
irb(main):036:1> Thread.new(i) do |a|
irb(main):037:2* sleep 0.1
irb(main):038:2> puts "[#{i}-#{a}]"
irb(main):039:2> end
irb(main):040:1> end
=> 1..5
irb(main):041:0>
irb(main):042:0* [5-5][5-4][5-3][5-2][5-1]
As you see, "i" is the same var for all threads - only "a" is thread local.
Regards
robert