Thanks for all your help, I've just decided to do it as (using Guy's example):
#!/usr/bin/ruby
require 'thread'
arr = [1,2,3,4]
mutex = Mutex.new
def arr.each(mutex)
0.upto(length-1) do |i|
Thread.new(self[i]) {|val|
sleep(rand .1)
mutex.synchronize {
yield val
}
}
end
end
vals = []
val = nil
arr.each(mutex) {|val|
vals << val
sleep(rand .1)
vals << (val + 10)
}
Thread.list.each do |aThread|
aThread.join if aThread != Thread.current
end
puts vals
% ruby test_mutex.rb
3
13
2
12
4
14
1
11
% ruby test_mutex.rb
4
14
3
13
1
11
2
12
--
Norman