> If you are concerned about this, then use a mutex around code that might
> corrupt your data:
>
> m = Mutex.new
> arr.each do |val|
> @mutex.synchronize do
> vals << val
> end
> end
>
> I think appending onto vals like this should be an atomic operation (so
> the mutex isn't necessary), but if you had a non-atomic operation, then
> you would definitely need a mutex.
How do I know what operations are atomic operations in ruby? Should I assume,
unless explicitly specified, that all operations in ruby are not atomic?
Actually my main concern is the yield below:
> def arr.each
> 0.upto(length-1) do |i|
> Thread.new(self[i]) {|val|
> sleep(rand .1)
> yield val
> }
> end
> end
I'm not so concerned about the order of the yields--obviously, that will be
undeterministic. What I'm concerned about is that with arr.each, is it possible
that the yield will happen at the same time as another yield, messing things up
somehow? Or, are yields atomic as well? The reason I'm doing this is that each
loop in the iterator might (can't predict) take a very long or short time to
finish and I want to process things as soon as they become available (for
example if the first iteration has a very long process and the second has a very
short process, then I want to thread both and have the first block be from the
second short process's yield).
Thanks,
Norman