From: Bob Bobrov Date: 2008-06-19T06:23:13+09:00 Subject: Threads and synchronized access to an array Hi everyone! A n00b question - I have a following ruby code: =============================================== @arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ] def iterate(thread_number) @arr.each do |line| puts "#{line} - I am thread number #{thread_number}" end end number_of_threads = 4 threads = [] number_of_threads.times do |t| threads[t] = Thread.new do iterate(t) end end threads.each {|t| t.join} ================================================ The output for this code is: ================================================ 1 - I am thread number 0 2 - I am thread number 0 3 - I am thread number 0 4 - I am thread number 0 5 - I am thread number 0 6 - I am thread number 0 7 - I am thread number 0 8 - I am thread number 0 1 - I am thread number 1 2 - I am thread number 1 3 - I am thread number 1 4 - I am thread number 1 5 - I am thread number 1 6 - I am thread number 1 7 - I am thread number 1 8 - I am thread number 1 1 - I am thread number 2 2 - I am thread number 2 3 - I am thread number 2 4 - I am thread number 2 5 - I am thread number 2 6 - I am thread number 2 7 - I am thread number 2 8 - I am thread number 2 1 - I am thread number 3 2 - I am thread number 3 3 - I am thread number 3 4 - I am thread number 3 5 - I am thread number 3 6 - I am thread number 3 7 - I am thread number 3 8 - I am thread number 3 ================================================ What should be changed in this code to make the output looks like this? ================================================ 1 - I am thread number #{thread_number} 2 - I am thread number #{thread_number} 3 - I am thread number #{thread_number} 4 - I am thread number #{thread_number} 5 - I am thread number #{thread_number} 6 - I am thread number #{thread_number} 7 - I am thread number #{thread_number} 8 - I am thread number #{thread_number} ================================================ I want to iterate the array values only one time by some number of threads, how should I control the access to array values? Each thread must be prohobited from using the array values, which have been already used by other threads. I was trying to change the code like this: @mutex = Mutex.new @arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ] @mutex.synchronize { def iterate(thread_number) @arr.each do |line| puts "#{line} - I am thread number #{thread_number}" end end } number_of_threads = 4 threads = [] number_of_threads.times do |t| threads[t] = Thread.new do iterate(t) end end threads.each {|t| t.join} and this: @mutex = Mutex.new @arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ] def iterate(thread_number) @arr.each do |line| puts "#{line} - I am thread number #{thread_number}" end end number_of_threads = 4 threads = [] number_of_threads.times do |t| threads[t] = Thread.new do @mutex.synchronize { iterate(t) } end end threads.each {|t| t.join} -- Posted via http://www.ruby-forum.com/.