Threads and class variables

A

Alex Ciarlillo

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) {
puts "This threads variables: #{period[0]} #{period[1]}
#{period[2]}"
conn = Optim.new
conn.connect('user', 'pass')
res = conn.rtrvReport('SALES', date, date, period[1], period[2],
"C:\\Reporter\\data\\#{period[0]}_#{date.gsub('/','-')}.txt", locations)
if res
puts "Retrieved #{period[0]}_#{date.gsub('/','-')}"
else
puts "Failed on #{period[0]}_#{date.gsub('/','-')}"
end
}
end
threads.each { |aThread| aThread.join }

The rtrvReport method isnt fully implemented but I am trying to simply
print out the sTime variable to make sure each thread is passing a
different sTime. Here is what I have:

def rtrvReport(reportType, sDate, eDate, sTime, eTime, reportPath,
locations)
puts "Got this: #{sTime}"
end

sTime is passed from period[1]. The problem is even though in my for
loop for the thread creation it is reporting the correct variables but
in this little output statement it is printing the same variable. So how
can each thread be passing this method different variables but each time
it prints out the same one!?!? Here is sample output:

This threads variables: breakfast 12:00:a 11:00:a
This threads variables: breakfast 11:01:a 4:00:p
This threads variables: breakfast 4:01:p 11:59:p
Got this: 4:01:p
Got this: 4:01:p
Got this: 4:01:p

So any ideas on why this is? Also, anyone know a shorter/cleaner way to
setup the 4 arrays of data I'm using for the threads?
Thanks,
-Alex
 
R

Robert Klemme

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
 
I

Ilan Berci

Alex said:
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) {
puts "This threads variables: #{period[0]} #{period[1]}
#{period[2]}"

So any ideas on why this is? Also, anyone know a shorter/cleaner way to
setup the 4 arrays of data I'm using for the threads?
Thanks,
-Alex

Alex,

Your thread is not passing it's constructor arg to the block as you are
not accepting it..
try:
threads << Thread.new(period) do | period|
puts "This threads variables: #{period[0]} #{period[1]}
end

p.s It's idiomatic to use do.. end for multiline blocks vs {}.. see
Pragmatic Guide to Ruby, Thomas, Hunt

as for making the arrays simpler, you could do:
periods = [%w(breakfast, 12:00, 13:00), %w(lunch, 14:00, 15:00),
%w{supper, 16:00, 17:00)]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top